how to measure running time of algorithms in python


Possible Duplicates:
Accurate timing of functions in python
accurately measure time python function takes

How can i mesure and compare the running times of my algorithms written in python .Also point me to a nice algorithms site/forum like stackoverflow if you can.

Asked By: Bunny Rabbit



The module timeit is useful for this and is included in the standard Python distribution.


import timeit
timeit.Timer('for i in xrange(10): oct(i)').timeit()
Answered By: Mark Byers

For small algorithms you can use the module timeit
from python documentation:

def test():
    "Stupid test function"
    L = []
    for i in range(100):

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()

Less accurately but still valid you can use module time like this:

from time import time
t0 = time()
t1 = time()
t2 = time()

print 'function vers1 takes %f' %(t1-t0)
print 'function vers2 takes %f' %(t2-t1)
Answered By: joaquin

The programming language doesn’t matter; measuring the runtime complexity of an algorithm works the same way regardless of the language. Analysis of Algorithms by Stanford on Google Code University is a very good resource for teaching yourself how to analyze the runtime complexity of algorithms and code.

If all you want to do is measure the elapsed time that a function or section of code took to run in Python, then you can use the timeit or time modules, depending on how long the code needs to run.

Using a decorator for measuring execution time for functions can be handy. There is an example at

Below I’ve shamelessly pasted the code from the site mentioned above so that the example exists at SO in case the site is wiped off the net.

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)

// John

Answered By: John P

I am not 100% sure what is meant by “running times of my algorithms written in python”, so I thought I might try to offer a broader look at some of the potential answers.

  • Algorithms don’t have running times; implementations can be timed, but an algorithm is an abstract approach to doing something. The most common and often the most valuable part of optimizing a program is analyzing the algorithm, usually using asymptotic analysis and computing the big O complexity in time, space, disk use and so forth.

    A computer cannot really do this step for you. This requires doing the math to figure out how something works. Optimizing this side of things is the main component to having scalable performance.

  • You can time your specific implementation. The nicest way to do this in Python is to use timeit. The way it seems most to want to be used is to make a module with a function encapsulating what you want to call and call it from the command line with python -m timeit ....

    Using timeit to compare multiple snippets when doing microoptimization, but often isn’t the correct tool you want for comparing two different algorithms. It is common that what you want is asymptotic analysis, but it’s possible you want more complicated types of analysis.

  • You have to know what to time. Most snippets aren’t worth improving. You need to make changes where they actually count, especially when you’re doing micro-optimisation and not improving the asymptotic complexity of your algorithm.

    If you quadruple the speed of a function in which your code spends 1% of the time, that’s not a real speedup. If you make a 20% speed increase on a function in which your program spends 50% of the time, you have a real gain.

    To determine the time spent by a real Python program, use the stdlib profiling utilities. This will tell you where in an example program your code is spending its time.

Answered By: Mike Graham
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.