Timing your Python Code

cnu profile image Srinivasan Rangarajan Originally published at cnu.name ใƒปUpdated on ใƒป2 min read

There are many times when you would want to see how much time your program takes to execute. The easiest way to do it on a unix system is to use the time command before running the program.

$ time command

real    0m1.320s
user    0m0.010s
sys     0m0.023s

This shows that the command took 1.32 seconds of wall clock time and 0.01s of time spent in userspace and 0.023s of time spent in kernel space.

This works well for any command you can run on the shell. But how would you find out how long a python function took? Or how long a particular line of code took?

The usual way I used to do was create a time.time() object before the piece of code, store it in a variable. Do the same time.time() and subtract from the previously stored value and print it.

Nothing wrong with it. But instead of repeating this same piece of code everywhere, there has got to be a better way.

Let me introduce the timing context manager.

Create a module called timer.py and paste the following piece of code in it.

import time

class Timer(object):
    def __init__(self, verbose=False):
        self.verbose = verbose

    def __enter__(self):
        self.start = time.time()
        return self

    def __exit__(self, *args):
        self.end = time.time()
        self.secs = self.end - self.start
        self.msecs = self.secs * 1000  # millisecs
        if self.verbose:
            print 'elapsed time: %f ms' % self.msecs

Now all you have to do is, in the module where you want to time a piece of code,

from timer import Timer
with Timer() as t:
print 'elapsed: %ss' % t.secs

You can use t.msecs to display it in milliseconds.

This is such a simple and elegant solution to log the time taken, that these kind of modules will probably have to go in a private python-utils package which I can import in any project I want.



Editor guide
rhymes profile image

Nice article! Just a quick note you might be interested into:

Although time.time() is perfectly fine you can also use timeit.default_timer() if you want because it is setup so that it uses the most precise timer function on each OS.

rpalo profile image
Ryan Palo

You probably already know about this, but if you have luxury of ipython, you can use the %timeit magic!

In [2]: %timeit [math.sin(x) for x in range(5000)]
1000 loops, best of 3: 719 ยตs per loop
cnu profile image
Srinivasan Rangarajan Author

Yes I know in ipython you can do that. But I wrote this module so that I can import it into my modules/projects and log the time taken for a particular function.

rpalo profile image
Ryan Palo

Gotcha. Yeah, your way works great for logging purposes.

vergeev profile image
Pavel Vergeev

And with the help of ContextDecorator you can enable the users of the class to use it as both decorator and context manager.