Slide 13
Slide 13 text
Python Profiling and Performance - Mahmoud Hashemi
The cProfile module
Simpler tools assume you know which code you want to measure,
whereas the built-in cProfile module:
● Measures a whole thread of execution
● Identifies time-consuming functions
● Command-line and programmatic interfaces
Command line
$ python -m cProfile target_code.py
Python
import cProfile, pstats
pr = cProfile.Profile()
pr.enable()
# call the code you want to measure
pr.disable()
pstats.Stats(pr).sort_stats('time').print_stats()
Offline Python profiling
624865 function calls (517453 primitive calls) in 0.289 seconds
Ordered by: internal time
List reduced from 44 to 10 due to restriction <10>
ncalls tottime percall cumtime percall filename:lineno(function)
2989/7 0.054 0.000 0.186 0.027 serdes.py:365(vo_tree)
12618/45 0.040 0.000 0.106 0.002 serdes_bin.py:78(append_field)
16393 0.037 0.000 0.044 0.000 serdes.py:315(_uniq_field)
84450/48 0.028 0.000 0.186 0.004 serdes.py:348(field_tree)
84450 0.023 0.000 0.037 0.000 {getattr}
68389 0.014 0.000 0.014 0.000 field.py:116(__get__)
26182 0.012 0.000 0.017 0.000 serdes_bin.py:128(append_int)
2757 0.009 0.000 0.010 0.000 serdes.py:336(_uniq_vo)
2489/43 0.008 0.000 0.105 0.002 serdes_bin.py:205(append_vo)
5103/94 0.007 0.000 0.103 0.001 serdes_bin.py:192(append_list)
The cProfile + pstats text output is utilitarian, but interesting
visualizations can be created with RunSnakeRun, SnakeViz,
Gprof2dot, pyprof2calltree, and pyinstrument.