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 @timemeth before @profileit(0) will measure consumed time of profileit method call.
  • swap @timemeth with @profileit(0) and you will profile the timemeth function
  • 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.