2. introduction to the profiler
• A profiler is a program descibes the run time
performance of a program.
• Python standard library provide 3 difference
profiler:
– cProfile : is recommende for most users.
– profile: a pure python module whose interface
imitated by cProfile
– hotshot: an experimental C module that focused
on minimizing the overhead of profiling
3. Quick example
Profile an application
1. use command line:
$ python -m cProfile myscript.py
2. or add into code with main function:
impor cProfile
cProfile.run(‘main()’)
The result looks
like:
4. Cython profiling basic
Enable it for source file
• profiling can be enabled by
adding at the top of file
#cython: profile=True
• Use a special decorator
disable profiling for one
function only:
cimport cython
@cython.profile(False)
def my_func():
pass
Use pstats module review the
profile
import pstats
p = pstats.Stats(‘fooprof’)
p.strip_dirs().sort_stats(‘time’)
.print_stats()