Skip to content

Instantly share code, notes, and snippets.

@alecordev
Created December 13, 2018 12:35
Show Gist options
  • Save alecordev/91d1546c453836f82f817bb86b8b174e to your computer and use it in GitHub Desktop.
Save alecordev/91d1546c453836f82f817bb86b8b174e to your computer and use it in GitHub Desktop.
Benchmarking and Profiling Python

High Performance Python

Overview

Performance?

  • Memory
  • CPU/GPU
  • I/O

Improve:

  • Follow best practices
  • Know your tool (i.e. Python)
  • Algorithms

There is a point where performance enhancements will only have as bottleneck the algorithms, heuristics, assumptions you make.

Profiling and Benchmarking

Python

timeit module and magic in Jupyter Notebooks.

  • $ python -m cProfile -s cumtime module_to_profile.py
  • $ python -m cProfile -o output.pstats module_to_profile.py - create an output.pstats file to be used elsewhere.

Libraries

Using the Libraries

gprof2dot

Use the output.pstats that is created using Python's cProfile module to create a graphviz compatible dot file.

$ python -m gprof2dot -f pstats output.pstats >> stats.dot

You can use graphviz from the command line to create a diagram of the analysis by dot -Tpng -o output.png stats.dot.

QCacheGrind

$ pyprof2calltree -i output.pstats -o filename.grind - and you can use https://sourceforge.net/projects/qcachegrindwin/ to open generated filename.grind to explore and perform profiling.

SnakeViz

$ snakeviz output.pstats - input is the cProfile file output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment