I prefer Viztracer for Python flamegraph profiling.
See also
There are many ways to use it, and excellent documentation in the above links, but some brief notes:
$ py -m viztracer program.pyA resultant result.json will be generated.
$ vizviewer result.jsonor if your Python install does not map scripts into the system PATH
$ py -m viztracer.viewer result.jsonSide-by-side comparisons
You can view two reports at once to do a side-by-side analysis of two runs. Just make sure to supply
--port 9002to one of them (default port is 9001).
It also supports inline usage:
import viztracer
foo()
bar()
baz()
tracer = viztracer.VizTracer()
tracer.start()
whats_goin_on_in_here()
tracer.stop()
tracer.save()Disable color printing
If your environment cannot handle colored printing, then add
from viztracer import util
util.color_support = Falsesomewhere in the pipeline. I’m not sure if there’s a CLI switch to do this or not.