SnakeViz
SnakeViz is a web-based viewer for Python profiler output, specifically designed for data generated by the `cProfile` module. It provides interactive graphical visualizations (icicle and sunburst charts) to help identify performance bottlenecks in Python code. The current version is 2.2.2, with releases occurring periodically to add features and address compatibility.
Warnings
- gotcha SnakeViz is primarily designed to work with profile data generated by Python's `cProfile` module. It does not support output from the older `profile` module.
- gotcha For very large or complex profile files, SnakeViz may struggle to create a visualization, sometimes displaying an error. This is due to the complexity of rendering an extremely large call tree.
- gotcha `cProfile`'s data format does not always allow a perfect reconstruction of the exact call stack and time spent, especially when the same function is called multiple times from different parts of the code. This can sometimes lead to slightly counter-intuitive visualizations.
- breaking Official support for Python 2.7 has been dropped. While older versions (v2.1.2 and earlier) might still function, newer versions (2.2.0+) are built for Python 3.9+.
- gotcha When running SnakeViz via IPython magics outside of a Jupyter Notebook environment (e.g., in a terminal IPython session), the visualization will open in a new browser tab by default. Within Jupyter, it typically embeds in the notebook.
Install
-
pip install snakeviz
Imports
- Command Line
snakeviz program.prof
- IPython Magic
%load_ext snakeviz %snakeviz my_function()
Quickstart
# 1. Profile your Python script using cProfile
import cProfile
import time
def waste_time():
sum(range(1000000))
def main():
for _ in range(5):
waste_time()
cProfile.runctx('main()', globals(), locals(), 'output.prof')
# 2. Open the profile in SnakeViz from your terminal
# (Run this in your shell, not in Python directly)
# !snakeviz output.prof