Profiling Python

Kai Riedmiller

The Problem

My program is slow, and I don’t know what it’s doing!

Goal:

Don’t do this, and it will be fast!

How?

Understand where time is spend during execution

Profiling

  1. Gather data
    • short runs: deterministic: cProfile
    • longer runs: statistical: Austin
  2. Analyze data
    • flame graph/icicles, sunburst: SnakeViz or Speedscope
    • call graph

Gathering data: cProfile

python -m cProfile -o cprofile.dat <PROGRAM>.py

  • Build-in tool
  • Deterministic
  • No multiprocessing/threading support

Gathering data: Austin

sudo apt install austin
austin python <PROGRAM>.py

  • Statistical profiler
  • Supports child processes
  • Supports attaching to running processes

Visualization: SnakeViz

Works with profiles generated by cProfile.

pip install snakeviz
python -m cProfile -o cprofile.dat <PROGRAM>.py
snakeviz cprofile.dat

icicle

icicle

sunburst

sunburst

Visualization: Callgraph

callgraph

callgraph

Visualization: Callgraph

gprof2dot

$ sudo apt-get install graphviz
$ git clone https://github.com/jrfonseca/gprof2dot
$ ln -s "$PWD"/gprof2dot/gprof2dot.py ~/bin
$ cd $PROJECT_DIR
$ gprof2dot.py -f pstats cprofile.dat | dot -Tsvg -o callgraph.svg

(pycallgraph is a python specific alternative)

Thanks

Find this presentation at karie.eu/python_profiling

..and soon in our wiki ;)