Profiling in Python

Lot of information is available in the Python documentation website: http://docs.python.org/library/profile.html . However, here is a small snapshot of how to use it :

Let us say you have a function call test(). First you need to run it:

import cProfile
cProfile.run('from your_module import test; test()', 'prof')

It will save profiling results in ‘prof’, which can then be parsed with pstats module:

import pstats
p = pstats.Stats('fooprof')

You can then introspect the data. For instance the following code sort the results by time and print the 10 largest one.

p.sort_stats('time').print_stats(10)
Please follow and like us:
This entry was posted in Python and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.