5.6. Tools Perf

  • A powerful Python profiler that can be used to profile Python code and C extensions.

  • Linux and macOS only.

$ PYTHONPERFSUPPORT=1 perf record -F 9999 -g -o mydata.perf python myfile.py
$ perf report -g -i mydata.perf
$ perf record -F 9999 -g -o mydata.perf python -X perf myfile.py
$ perf report -g -i mydata.perf

or:

import sys

sys.activate_stack_trampoline("perf")
do_profiled_stuff()
sys.deactivate_stack_trampoline()

non_profiled_stuff()

then:

$ perf record -F 9999 -g -o mydata.perf python myfile.py
$ perf report -g -i mydata.perf

or with JIT:

$ perf record -F 9999 -g -k 1 --call-graph dwarf -o mydata.perf python -Xperf_jit myfile.py
$ perf inject -i mydata.perf --jit --output mydata-jit.perf
$ perf report -g -i mydata-jit.perf

or with JIT:

$ PYTHON_PERF_JIT_SUPPORT=1 perf record -F 9999 -g --call-graph dwarf -o mydata.perf python myfile.py
$ perf inject -i mydata.perf --jit --output mydata-jit.perf
$ perf report -g -i mydata-jit.perf