cprofile_graph is used to generate visual graphs for Python profiling.
- First you need to install Graphviz, use
sudo apt-get install graphviz graphviz-dev
in Ubuntu or see this link for other platforms. - Use pip to install
cprofile_graph
.
pip install cprofile_graph
- As a context manager:
from cprofile_graph import profile_ctx
with profile_ctx("myprofile.png"):
foo()
- As a function wrapper:
@profile_decorator
def foo():
1 + 1
@profile_decorator("myprofile.png")
def foo():
1 + 1
- As a function:
from cprofile_graph import profile_code
profile_code("foo()", "myprofile.png")
- As a script:
cprofile_graph myscript.py -o myprofile.png
cprofile_graph -c "foo()" -o myprofile.png
This is the kind of output you will get from cprofile_graph, hot colors indicate most used functions:
- gprof2dot: Convert cProfile stats into dot files.
- Graphviz: Convert dot files into visual graphs.
- pygraphviz: Call Graphviz from within Python.