gprof2dot
gprof2dot is a Python script that converts the output from various profilers (e.g., cProfile, gprof, Valgrind callgrind, Linux perf, Java HPROF) into a Graphviz dot graph. This allows for visual analysis of performance bottlenecks and call flows within an application. The library is actively maintained with a current version of 2025.4.14, typically releasing updates on a yearly or bi-yearly basis.
Warnings
- gotcha gprof2dot requires an external tool, Graphviz, to render the generated '.dot' files into visual graphs (like PNG or SVG). `pip install gprof2dot` does NOT install Graphviz. You must install Graphviz separately (e.g., `sudo apt-get install graphviz` on Debian/Ubuntu, or from its official website). Without Graphviz, the `dot` command will not be found, and image generation will fail.
- breaking Support for Python 2.x has been dropped. gprof2dot now officially requires Python 3.8 or newer. Attempts to run on older Python versions will likely result in errors.
- gotcha When profiling C/C++ code with `gprof`, the resulting call graph data can sometimes be inaccurate or flaky. This can lead to misleading visualizations where the 'main' function isn't the root, or library functions appear as top-level callers. This is a limitation of `gprof` itself, not `gprof2dot`.
- gotcha Node labels in the generated graphs can become excessively long, especially when profiling C++ code with complex template names, scope information, or function arguments. This can make the graph unreadable.
- gotcha The project maintainer has indicated having 'little or no time for its maintenance', implying that new features are unlikely and response times for issues/pull requests may be slow. While the tool remains functional and receives occasional updates, active development for new features is not a priority.
Install
-
pip install gprof2dot
Imports
- gprof2dot
import gprof2dot
Quickstart
# 1. Profile your Python code (e.g., using cProfile)
import cProfile
import time
def my_function():
time.sleep(0.1)
another_function()
def another_function():
time.sleep(0.05)
if __name__ == "__main__":
profiler = cProfile.Profile()
profiler.enable()
my_function()
profiler.disable()
profiler.dump_stats("output.pstats")
print("Profile data saved to output.pstats")
# 2. Convert the .pstats file to a .dot graph and render with Graphviz
# Run this from your terminal after the Python script above:
# gprof2dot -f pstats output.pstats | dot -Tpng -o output.png
# (Ensure 'dot' command from Graphviz is in your PATH)