Python profile decorator
1 2 # Maxime Biais <http://www.biais.org/blog> 3 4 import hotshot, hotshot.stats 5 6 def profileit(printlines=1): 7 def _my(func): 8 def _func(*args, **kargs): 9 prof = hotshot.Profile("profiling.data") 10 res = prof.runcall(func, *args, **kargs) 11 prof.close() 12 stats = hotshot.stats.load("profiling.data") 13 stats.strip_dirs() 14 stats.sort_stats('time', 'calls') 15 print ">>>---- Begin profiling print" 16 stats.print_stats(printlines) 17 print ">>>---- End profiling print" 18 return res 19 return _func 20 return _my
Usage:
1 2 @profileit(20) 3 def mop(): 4 a = 0 5 for i in range(100): 6 a += mip() 7 return a 8 print mop()