Sub topic 4
Reading profiles: tottime, cumulative time, and callers
This chapter assumes you can run a script under cProfile (covered in the previous chapter) and are comfortable with Python functions, loops, and the terminal. Here you will learn to read the report, tell a real hot spot from a function that only looks expensive, and find who calls it.
The columns in a profile
A cProfile report has one row per function. These are the columns you need:
| Column | Meaning |
|---|---|
| `ncalls` | How many times the function was called. |
| `tottime` | Time spent inside the function itself, excluding calls to other functions. |
| `percall` (first) | `tottime` divided by `ncalls`. |
| `cumtime` | Time spent in the function plus everything it called. |
| `percall` (second) | `cumtime` divided by `ncalls`. |
The key distinction is between tottime and cumtime. A function like main has a large cumtime because it calls everything else, but its tottime is tiny. It is not slow. It is only the entry point of the slow work.
A worked example
This script has one function that does real work and one that only delegates.
import cProfile
import pstats
def parse(line):
return [int(x) for x in line.split(",")]
def process(lines):
return [sum(parse(line)) for line in lines]
def main():
lines = ["1,2,3,4,5,6,7,8,9,10"] * 50_000
return process(lines)
with cProfile.Profile() as profiler:
main()
stats = pstats.Stats(profiler)
stats.sort_stats("tottime").print_stats(5)Sort by tottime and the top rows are the functions doing the work: the list comprehension inside parse, parse itself, and str.split. Sort by cumulative and main and process rise to the top, because they contain everything below them.
Use each sort for a different question:
- Sort by
tottimeto find where the CPU time is actually spent. This points at code you can change. - Sort by
cumulativeto find which branch of the program is expensive. This tells you where to look.
Finding callers and callees
A hot function is often fast per call but called far too often. ncalls shows this. When you find a suspicious function, ask who calls it. pstats can print both directions:
stats.print_callers("parse")
stats.print_callees("process")print_callers lists every function that called parse, with call counts and time. print_callees lists what process called. The argument is a regular expression matched against function names, so "parse" also matches any other function whose name contains that text.
Common mistakes
- Optimizing the top cumulative row.
mainis always at the top. Improving it means improving something beneath it. - Ignoring
ncalls. A function taking 2 microseconds per call but called 10 million times costs 20 seconds. The fix is to call it less, not to speed up the body. - Trusting tiny profiles. Profile with realistic input sizes. A function that is negligible on 100 items may dominate on 1,000,000.
- Forgetting profiler overhead. cProfile adds cost to every function call, so programs with many tiny calls look slower than they are. Confirm any fix with
timeitorperf_counter, not with profiler totals.
Exercise
A profile shows main with cumtime 9.8 s and tottime 0.001 s, and score with cumtime 7.1 s and tottime 6.9 s. Which function should you inspect first, and why?
Show answer
Inspect score. Its tottime of 6.9 s is almost equal to its cumtime, so nearly all its time is spent in its own code rather than in functions it calls. main has a tiny tottime, so it only delegates work to other functions.
Exercise
Which pstats method tells you which functions are calling a hot function parse, and what does ncalls help you decide?
Show answer
stats.print_callers("parse") lists the callers. A high ncalls suggests the better fix is to reduce how often parse is called, for example by caching or restructuring the loop, instead of optimizing its body.