Phase 20 of 25 · Topic 20.1

Profiling Python Code with cProfile & pstats

1Concept

`cProfile` is a deterministic C-implemented profiler that records function call counts and execution times (`tottime`, `cumtime`), pinpointing execution bottlenecks.

2Architecture Diagram

python -m cProfile -s cumtime script.py ---> Displays slowest functions sorted by cumulative time

3Code Example

Python 3.12
import cProfile
import pstats
import io

def heavy_computation():
    return sum(i * i for i in range(100_000))

profiler = cProfile.Profile()
profiler.enable()
heavy_computation()
profiler.disable()

stream = io.StringIO()
stats = pstats.Stats(profiler, stream=stream).sort_stats('cumtime')
stats.print_stats(3)
print("=== cProfile Output (Top Hotspots) ===")
print(stream.getvalue()[:280] + "...")

4Expected Output

=== cProfile Output (Top Hotspots) ===
         4 function calls in 0.007 seconds

   Ordered by: cumulative time

   ncalls  toptime  cumtime  percall filename:lineno(function)
        1    0.000    0.007    0.007 <ipython-input>:4(heavy_computation)
        1    0.007    0.007    0.007 <ipython-input>:5(<genexpr>)...

5Key Takeaways

  • `tottime` is time spent in the function itself; `cumtime` includes time in sub-functions called.
  • Use `line_profiler` for line-by-line timing of hot functions.
  • Never optimize without measuring first: Premature optimization is the root of all evil.