Profiling Python Code

Figure out the bottlenecks in your Python scripts.

Run Your Code with cProfile

python -m cProfile -o my_output.cprof

Visualize Your Code’s Execution

# visualize with pyprof2calltree
pip install pyprof2calltree
pyprof2calltree -k -i my_output.cprof

# visualize with pyprof2calltree
pip install snakeviz
snakeviz my_output.cprof
  • ncalls: number of calls,
  • tottime: total time spent in the given function (excluding time made in calls to sub-functions)
  • percall: quotient of tottime divided by ncalls
  • cumtime: total time spent in this and all subfunctions
  • percall: quotient of cumtime divided by primitive calls