29

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.

Community
  • 1
  • 1
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
  • 2
    Similar to http://stackoverflow.com/questions/889900/accurate-timing-of-functions-in-python – Mark Byers Apr 18 '10 at 12:07
  • Both of those threads were about timing *functions*, which may or may not be what is required to do "timing algorithms", which seems to me to be an ill-defined task. It might require a lot more complex things than timing a function. – Mike Graham Apr 18 '10 at 22:32
  • Also, questions about algorithms (as long as they're programming related) are certainly welcome on Stack Overflow. – Greg Hewgill Apr 19 '10 at 05:54
  • Similar to https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python/52288622#52288622 – Shital Shah Sep 12 '18 at 06:31

5 Answers5

28

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

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

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()
call_mifuntion_vers_1()
t1 = time()
call_mifunction_vers_2()
t2 = time()

print 'function vers1 takes %f' %(t1-t0)
print 'function vers2 takes %f' %(t2-t1)
joaquin
  • 82,968
  • 29
  • 138
  • 152
  • +1 for showing how to run timeit on a function in __main__. Useful when comparing several different methods of doing something – Patrick Sep 07 '14 at 10:29
  • 2
    Although a link to where you got it from whould have been even more useful... ;-) https://docs.python.org/2/library/timeit.html#examples – Patrick Sep 07 '14 at 10:35
25

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

Example:

import timeit
timeit.Timer('for i in xrange(10): oct(i)').timeit()
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
19

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.

Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • 57
    -1 for telling him you don't understand his question to make some esoteric point, and then proceeding to answer his questions, which you understood all along. – Greg Feb 19 '17 at 19:05
13

Using a decorator for measuring execution time for functions can be handy. There is an example at http://www.zopyx.com/blog/a-python-decorator-for-measuring-the-execution-time-of-methods.

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):

    @timeit
    def foo(self, a=2, b=3):
        time.sleep(0.2)

@timeit
def f1():
    time.sleep(1)
    print 'f1'

@timeit
def f2(a):
    time.sleep(2)
    print 'f2',a

@timeit
def f3(a, *args, **kw):
    time.sleep(0.3)
    print 'f3', args, kw

f1()
f2(42)
f3(42, 43, foo=2)
Foo().foo()

// John

John P
  • 15,035
  • 4
  • 48
  • 56
  • Is it possible to pass the timeit results back to the calling process; or otherwise output the timeit result after calling the method? (The time being output before the calls stdout threw me off) – ThorSummoner Mar 16 '15 at 05:21
  • Here's more refined version: https://stackoverflow.com/a/52288622/207661 – Shital Shah Sep 12 '18 at 06:32
  • 1
    Would `time.process_time` be better choice if we want to rule out influence of other processes that may be running during test? – Greg0ry May 06 '22 at 18:22
-1

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.

elixenide
  • 44,308
  • 16
  • 74
  • 100
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • 1
    The question is about the running time, not the actual computational time. – SeF Dec 09 '16 at 09:15
  • @SeF, you may have interpreted it that way, but it's not explicit in the question. And given that it says "algorithm" rather than "implementation", the interpretation assumed by this answer is within reason. – Michael Aaron Safyan Dec 09 '16 at 13:17
  • @SeF, on top of the above, given the ambiguity, my answer addresses both interpretations of the question (my answer also mentions timeit), and this answer predates the ones that go more in depth on using the timeit module. – Michael Aaron Safyan Dec 09 '16 at 13:20