profiling in python
TRANSCRIPT
PROFILING IN PYTHON
JohnThursday, April 13, 2023
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
Quick example
Profile an application1. use command line:$ python -m cProfile myscript.py
2. or add into code with main function:impor cProfilecProfile.run(‘main()’)
The result looks like:
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:
Use pstats module review the profile
import pstatsp = pstats.Stats(‘fooprof’)p.strip_dirs().sort_stats(‘time’).print_stats()
cimport [email protected](False)def my_func():
pass
Reference
• http://docs.python.org/2/library/profile.html