Profile a Python Hot Function with cProfile
Use cProfile and pstats to rank functions by cumulative time and fix the real hot path instead of guessing.
Task
Profile a slow Python entry point to find the function that dominates runtime.
When to use this
A request handler is slow and the team optimises the wrong function based on intuition.
Prerequisites
- Python 3 with cProfile
- A reproducible slow workload
- A representative dataset
Steps
1. Profile the entry point and sort by cumulative time.
import cProfile, pstats
cProfile.run('main()', 'prof.out')
stats = pstats.Stats('prof.out').sort_stats('cumtime')
stats.print_stats(15)Expected output: The top fifteen functions by cumulative time reveal the dominant path.
2. Focus on the top offender and re-profile.
python -m cProfile -s cumtime app.py | head -30Expected output: After the fix the offender drops out of the top list, confirming the change.
Troubleshooting
Problem
Profiler overhead distorts results
Solution
Compare relative ranking, not absolute times, and use sampling profilers for long runs.
Problem
Hot path hidden in C extension
Solution
Profile with line-level tools if the time is inside native calls.
Limitations
- cProfile adds overhead.
- Does not show memory allocation.
- Line-level attribution needs extra tooling.
What this recipe does
Profile a slow Python entry point to find the function that dominates runtime.
A request handler is slow and the team optimises the wrong function based on intuition.
Prerequisites
- Python 3 with cProfile
- A reproducible slow workload
- A representative dataset
Procedure
- Profile the entry point and sort by cumulative time.
import cProfile, pstats
cProfile.run('main()', 'prof.out')
stats = pstats.Stats('prof.out').sort_stats('cumtime')
stats.print_stats(15)
Expected result: The top fifteen functions by cumulative time reveal the dominant path.
- Focus on the top offender and re-profile.
python -m cProfile -s cumtime app.py | head -30
Expected result: After the fix the offender drops out of the top list, confirming the change.
How to confirm it worked
Run the commands above and confirm every expected result in order. If a step does not produce the stated output, use the troubleshooting notes before continuing. A passing run shows after optimising the top cumulative function, the end-to-end runtime drops and the ranking changes as expected.
Troubleshooting
Profiler overhead distorts results — Compare relative ranking, not absolute times, and use sampling profilers for long runs.
Hot path hidden in C extension — Profile with line-level tools if the time is inside native calls.
Limitations
- cProfile adds overhead.
- Does not show memory allocation.
- Line-level attribution needs extra tooling.