liveBook · Manning

cProfile

As explained in Fast Python: High performance techniques for large datasets

cProfile is a Python standard-library deterministic profiler that records function-call counts and execution-time statistics.

cProfile is Python’s built-in deterministic profiler for locating bottlenecks before optimization. It records function-call counts and timing statistics without requiring changes to the profiled program. section 2.1.2

Use the profiler output to interpret where execution time goes:

  • Sort results by cumulative time to find functions whose own work and subcalls consume the most time. section 2.1.2
  • Read ncalls for invocation frequency, tottime for time excluding subcalls, and cumtime for time including subcalls. section 2.1.2
  • Treat network and disk I/O as possible dominant costs, especially when timings vary between runs. section 2.1.2
  • Escalate to line profiling when function-level cumulative values cannot explain the bottleneck; cProfile is faster but less detailed. section 2.2.3

Profile a data-loading script

Run `python -m cProfile -s cumulative load.py ... > profile.txt`, then inspect the report. A data-loading run shows `download_all_data` dominating cumulative time, with network calls and socket reads accounting for much of that cost.

python -m cProfile -s cumulative load.py 01044099999,02293099999 2021-2021 > profile.txt

Use the runnable command to profile a script and save results ordered by cumulative time. section 2.1.2

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Reports calls and cumulative, inclusive, and exclusive function timings, but stops short of line-level breakdowns.
Examples
Uses a NOAA data downloader, cumulative-time reports, socket reads, and IPython timeit comparisons.
What it assumes
Assumes runnable Python scripts and basic familiarity with functions, calls, and execution time; teaches profiling interpretation from function statistics.
What you can do after
Diagnose/debug with it by running a script under cProfile and prioritizing functions or I/O paths with high cumulative time.
How it connects
Connects profiling with bottleneck analysis, line profiling, timeit, network I/O, and disk I/O.

Also explained in

Compare how 2 books explain cProfile →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.