Skip to content

Importing from pyinstrument (python)

Jamie Wong edited this page May 17, 2022 · 1 revision

pyinstrument is a statistical profiler for Python.

pyinstrument has an explicit output format for speedscope (See: https://github.com/joerick/pyinstrument/pull/160).

To profile a Python script and view the output in speedscope, run:

pyinstrument -r speedscope -o profile.speedscope.json your_script.py

Or, you can profile a specific block of code using:

from pyinstrument import Profiler
from pyinstrument.renderers import SpeedscopeRenderer

with Profiler() as profiler:
    ... # your code here

with open('profile.speedscope.json', 'w') as f:
    f.write(profiler.render(renderer=SpeedscopeRenderer()))

Then drop the resulting profile.speedscope.json into https://www.speedscope.app/