Skip to content
Jukka Lehtosalo edited this page Mar 28, 2022 · 2 revisions

Mypy is compiled using mypyc to C extension modules. This means that using the stdlib cProfile or profile module isn't going to be effective, unless you use a non-compiled mypy -- and in this case the results may not be indicative of performance when using a compiled mypy.

py-spy is a profiling tool that works with compiled mypy (at least in Linux).

Use it like this to profile mypy (replace -c 'import os' with your command line arguments):

$ pip install py-spy
$ pip install mypy
$ py-spy record --native -f speedscope -o profile.dat -- mypy -c 'import os'

Now open https://www.speedscope.app/, click Browse and import the profile.dat file you generated above. You can click 'Sandwich' to get a flat profile.

If the mypy run is relatively quick (less than a few seconds), consider using -r 500 with py-spy to increase the sampling rate.

Note: To get repeatable results, disable incremental mode by using mypy --no-incremental, or delete the .mypy_cache directory before each run.

If you are reporting a mypy performance issue or regression, feel free to add a link to the collected profile.dat (after you've verified with speedscope that it contains useful information) so that mypy developers can also analyze the profile.