How to Profile with cProfile

May 30, 2026 | 5 min read

Run cProfile

python -m cProfile -o profile.stats -s cumulative script.py

Analyse with pstats

python -m pstats profile.stats
> sort cumtime
> stats 20

Visualise with SnakeViz

pip install snakeviz
snakeviz profile.stats

Pick Targets

Select the top 5 functions by cumtime for compilation.