📅 2013-May-12 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cprofile, profiler, pstats, python ⬩ 📚 Archive
cProfile is one of the many profilers that ship with Python.
To profile a function in your Python program:
# Assuming we want to profile function doMain()
import cProfile
cProfile.run( "doMain( a, b )" )
When a function call is executed with the profiler, it prints out detailed profiling information after the function call finishes execution.
To dump the profiler statistics to a file named foo.stats:
# Assuming we want to profile function doMain()
import cProfile
cProfile.run( "doMain( a, b )", filename="foo.stats" )
The stats file is not human readable, but is written in the pstats format. This can be opened by using the pstats Python module in a script or using other tools that understand this format.
To profile a Python program named foo.py:
$ python -m cProfile foo.py
To profile a Python program and dump the profiler statistics to a file named foo.stats:
$ python -m cProfile -o foo.stats foo.py
Tried with: Python 2.7.6 and Ubuntu 14.04