Profiling Python with valgrind

1. Under Linux, install valgrind as follows (Fedora):

sudo yum install valgrind kcachegrind

and pyprof2calltree:

pip install pyprof2calltree

2. create a Python script (let us call it run.py)
3. Create the file containing the profile information using valgrind/callgrind

python -m cProfile  -o profile_data.pyprof run.py
pyprof2calltree -i profile_data.pyprof  # this converts the stats into a callgrind format

4. Visualisation

kcachegrind profile_data.pyprof.log

Alternatively, for visualisation, you can also use gprof2dot and graphviz (dot):

gprof2dot.py --format=callgrind --output=out.dot profile_data.pyprof.log
dot -Tsvg out.dot -o graph.svg
Please follow and like us:
This entry was posted in Python and tagged , . Bookmark the permalink.

One Response to Profiling Python with valgrind

Leave a Reply

Your email address will not be published.