September 17, 2009

A Python decorator for measuring the execution time of methods

As Python developer you often have the need for profiling and benchmarking applications. The standard tools are the Python profiler hotshot or standard debugging code with timestamp. Within one of our current project I had to track down performance issues related to LDAP connectivity. The goal was to measure the time spend within the connect() and search() method of the LDAPUserFolder implementation. As result I wrote a small @timeit decorator that allows you to measure the execution times of dedicated methods (module-level methods or class methods) by just adding the @timeit decorator in in front of the method call.

import time                                                

def timeit(method):

    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()

        print '%r (%r, %r) %2.2f sec' % \
              (method.__name__, args, kw, te-ts)
        return result

    return timed

class Foo(object):

    def foo(self, a=2, b=3):

def f1():
    print 'f1'

def f2(a):
    print 'f2',a

def f3(a, *args, **kw):
    print 'f3', args, kw

f3(42, 43, foo=2)


There is also the profilehooks module by Marius Gedminas basically doing something similar (in detail: it profiles the hooked method). It is perhaps the better alternative.