Tuesday 27 February 2007
Profiling the profiler
By Maxime Biais, Tuesday 27 February 2007 at 23:29 :: Python
A small snippet to measure profiling time:
from profileit import profileit import operator import time timed = 0 def timemeth(func): def _inner(self, *args, **kw): start = time.time() res = func(self, *args, **kw) global timed timed = time.time() - start return res return _inner @timemeth @profileit(0) def mip0(iterable): reduce(operator.add, iterable, 0) @timemeth @profileit(0) def mip1(iterable): reduce(lambda x, y: x + y, iterable, 0) mip0(xrange(100000)) print "timed %.3f seconds" % timed mip1(xrange(100000)) print "timed %.3f seconds" % timed
Notes:
- profileit is described here.
- putting
@timemethbefore@profileit(0)will measure consumed time ofprofileitmethod call. - swap
@timemethwith@profileit(0)and you will profile thetimemethfunction - read my last post to see why I used python2.5 and not python2.4
Outputs:
$ python2.5 reducecomp.py|grep seconds
1 function calls in 0.097 CPU seconds
timed 0.109 seconds
100001 function calls in 0.406 CPU seconds
timed 8.883 seconds
It's not a surprise, the profiler is time consuming when many functions are called.





