Created
May 8, 2014 15:35
-
-
Save ddm/e837207cb673024c245c to your computer and use it in GitHub Desktop.
Python profiling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The Python 2.7 documentation recommends the use of cProfile to profile Python code with reasonnably low overhead: https://docs.python.org/release/2.7/library/profile.html | |
If you use virtualenv, dont forget to activate it: | |
$ . bin/activate | |
How do you launch your script with cProfile enabled ? | |
$ python -m cProfile -o run.profile myscript.py | |
The output file (run.profile) will be generated when the execution terminates (including CTRL-C). | |
How do you analyse the results of your session? | |
1/ pstats | |
#!/usr/bin/env python | |
import pstats | |
pstats.Stats("run.profile").sort_stats("cumulative", "calls").print_stats() | |
2/ pyprof2calltree + kcachegrind | |
$ pip install pyprof2calltree | |
$ sudo apt-get install kcachegrind (Debian/Ubuntu) | |
or | |
$ brew install qcachegrind (OS X with Homebrew) | |
To launch KCacheGrind on run.profile: | |
$ pyprof2calltree -i run.profile -k | |
3/ gprof2dot + graphviz | |
$ pip install gprof2dot | |
$ sudo apt-get install graphviz (Debian/Ubuntu) | |
or | |
$ brew install graphviz (OS X with Homebrew) | |
To generate a png from your run.profile: | |
$ gprof2dot -f pstats run.profile | dot -Tpng -o output.png |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment