liveBook · Manning

cProfile

As explained in Fast by Default

cProfile is Python's built-in deterministic profiler that reports function-call counts and timing information.

cProfile is Python’s built-in function-level CPU profiler. It records function-call counts and timing information with minimal setup, helping locate application hotspots before release. section 7.2.3

Listing 7.3 Python CPU profiling with cProfile
import cProfile
import pstats
 
profiler = cProfile.Profile() #A
profiler.enable()
 
# Your code here
process_request(request_data) #B
 
profiler.disable()
stats = pstats.Stats(profiler)
stats.sort_stats('cumulative') #C
stats.print_stats(20)  #D

Wrap the suspected operation with cProfile, collect pstats output, sort by cumulative time, and inspect the highest-cost functions. section 7.2.3

Read the results by separating local work from downstream work:

  • tottime measures time spent inside a function, excluding calls it makes. section 7.2.3
  • cumtime includes the function and everything in its call chain. section 7.2.3
  • High cumtime with low tottime points deeper into the call chain; high tottime identifies work inside the function itself. section 7.2.3

Interpreting a slow request

If serialize_response reports 400 ms of cumtime but only 5 ms of tottime, inspect its callees rather than blaming serialization. A database function showing 310 ms of tottime is a stronger investigation target. section 7.2.3

How Fast by Default covers it

How deep it goes
Explains setup, sorted output, and tottime-versus-cumtime diagnosis, but defers line-level analysis to other profilers.
Examples
Uses request processing, serialize_response, execute_query, validate_token, and a cumulative-time statistics table.
What you can do after
Diagnose CPU bottlenecks by profiling a request, sorting statistics, and tracing high cumulative-time functions.
How it connects
Connects CPU hotspots to call trees, flame graphs, pstats, py-spy, and line-level profiling.

Also explained in

Compare how 3 books explain cProfile →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.