Timing functions in Python

Recently I saw a blogpost on tracking time in Python. This is inspired me to share how I track time in my python scripts. First I put the following python decorator function in my code or preferably import it from a common module.

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

You can use the timeit function like this:

def longRunningOperation(dummyArg, optionalDummyArg=None):
longRunningOperation("test", "optional test")

The output of the timeit decorator function looks like this:

'longRunningOperation' (('test', 'optional test'), {}) 3.00 sec

As you can see, the timit function reports the function name, the passed arguments and the elapsed time

If you want to learn more about decorator functions I would suggest to read the python.org wiki article on decorators. On the same site there is also an article with useful decorator functions for things like caching, properties, pre- and post conditions, memoization and many more.

Roundup January 2012

This post is a roundup post for the month january.


Just before the end of the year I created a tumblr blog where I shared some inspiring or interesting quotes.

On this blog I wrote 2 posts. The first post of january was my second post on developing a GeometryService in node.js. Some things I talked about where nodemonw for monitoring your files for changes and restarting node.js, node-inspector for debugging and vowsjs for writing tests. The second post of this month elaborated on the Geometry Visualizer that I created this month. The Geometry Visualizer uses the ArcGIS JavaScript API and openlayers to provide a .






New blogs to follow