DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Python Profile Decorator
Python profile decorator. More info on this blog post: <a href="http://www.biais.org/blog/index.php/2007/01/20/18-python-profiling-decorator">http://www.biais.org/blog/index.php/2007/01/20/18-python-profiling-decorator</a>
# Maxime Biais <http://www.biais.org/blog>
import hotshot, hotshot.stats
def profileit(printlines=1):
def _my(func):
def _func(*args, **kargs):
prof = hotshot.Profile("profiling.data")
res = prof.runcall(func, *args, **kargs)
prof.close()
stats = hotshot.stats.load("profiling.data")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
print ">>>---- Begin profiling print"
stats.print_stats(printlines)
print ">>>---- End profiling print"
return res
return _func
return _my
Usage:
@profileit(20)
def mop():
a = 0
for i in range(100):
a += mip()
return a
print mop()





