Profiling Python code cheatsheet

Software needed

  • For snakeviz profiling:

pip install snakeviz
  • For kcachegrind profiling:

pip install pyprof2calltree
sudo apt install kcachegrind

Enabling profiling via embedded cProfile

import cProfile
pr = cProfile.Profile()
pr.enable()
# ... lines to profile ...
pr.disable()
pr.dump_stats("/example-path/output.prof")

Profiling with snakeviz

snakeviz output.profile

Profiling with kcachegrind

pyprof2calltree -i output.prof -o callgrind.output.prof
kcachegrind callgrind.output.prof