64

I'm programming in python on windows and would like to accurately measure the time it takes for a function to run. I have written a function "time_it" that takes another function, runs it, and returns the time it took to run.

def time_it(f, *args):
    start = time.clock()
    f(*args)
    return (time.clock() - start)*1000

i call this 1000 times and average the result. (the 1000 constant at the end is to give the answer in milliseconds.)

This function seems to work but i have this nagging feeling that I'm doing something wrong, and that by doing it this way I'm using more time than the function actually uses when its running.

Is there a more standard or accepted way to do this?

When i changed my test function to call a print so that it takes longer, my time_it function returns an average of 2.5 ms while the cProfile.run('f()') returns and average of 7.0 ms. I figured my function would overestimate the time if anything, what is going on here?

One additional note, it is the relative time of functions compared to each other that i care about, not the absolute time as this will obviously vary depending on hardware and other factors.

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
Atilio Jobson
  • 719
  • 1
  • 8
  • 11
  • `time-it` allows you to add a decorator to any function to time it. It also accepts a logger name as a param so you can log the time `pip install time-it` disclaimer: I wrote the module – daniel blanco Nov 08 '22 at 21:14

7 Answers7

71

Use the timeit module from the Python standard library.

Basic usage:

from timeit import Timer

# first argument is the code to be run, the second "setup" argument is only run once,
# and it not included in the execution time.
t = Timer("""x.index(123)""", setup="""x = range(1000)""")

print t.timeit() # prints float, for example 5.8254
# ..or..
print t.timeit(1000) # repeat 1000 times instead of the default 1million
dbr
  • 165,801
  • 69
  • 278
  • 343
Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • 3
    i want my function to be called with different arguements, yet when i call t = timeit.Timer("f()", "from ___main___ import f") with different arguements and run t.timeit(10000) again, i get the same results though the different arguements should result in very different runtimes. – Atilio Jobson May 20 '09 at 20:47
39

Instead of writing your own profiling code, I suggest you check out the built-in Python profilers (profile or cProfile, depending on your needs): http://docs.python.org/library/profile.html

Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • Ignore me - that string is not a function name, it is a block of eval'd code. So you can use it for quick timing. This is the right answer. And in other news - "is not" is significantly quicker than "!=" - but may have other implications. – Danny Staple Jun 11 '12 at 10:20
  • 1
    And off on that tangent - before using "is not" for anything clever - bear this in mind - http://stackoverflow.com/questions/1392433/python-why-is-hello-is-hello – Danny Staple Jun 11 '12 at 10:41
  • 1
    The python profile docs say however: "The profiler modules are designed to provide an execution profile for a given program, not for benchmarking purposes (for that, there is timeit for reasonably accurate results)." – mmagnuski Oct 12 '18 at 13:11
36

You can create a "timeme" decorator like so

import time                                                

def timeme(method):
    def wrapper(*args, **kw):
        startTime = int(round(time.time() * 1000))
        result = method(*args, **kw)
        endTime = int(round(time.time() * 1000))

        print(endTime - startTime,'ms')
        return result

    return wrapper

@timeme
def func1(a,b,c = 'c',sleep = 1):
    time.sleep(sleep)
    print(a,b,c)

func1('a','b','c',0)
func1('a','b','c',0.5)
func1('a','b','c',0.6)
func1('a','b','c',1)
vdrmrt
  • 862
  • 8
  • 14
  • 1
    +n for this answer. I wish there was such an option. Beauty is I can export the log results to an external file and add this wherever I need to! Thanks a zillion. – curlyreggie Nov 04 '13 at 12:16
  • 8
    This works great for non-recursive functions. For recursive functions, it returns a timing for each iteration of the function. – tachijuan Aug 18 '14 at 15:26
  • Here's more refined version: https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python/52288622#52288622 – Shital Shah Sep 12 '18 at 06:30
24

This code is very inaccurate

total= 0
for i in range(1000):
    start= time.clock()
    function()
    end= time.clock()
    total += end-start
time= total/1000

This code is less inaccurate

start= time.clock()
for i in range(1000):
    function()
end= time.clock()
time= (end-start)/1000

The very inaccurate suffers from measurement bias if the run-time of the function is close to the accuracy of the clock. Most of the measured times are merely random numbers between 0 and a few ticks of the clock.

Depending on your system workload, the "time" you observe from a single function may be entirely an artifact of OS scheduling and other uncontrollable overheads.

The second version (less inaccurate) has less measurement bias. If your function is really fast, you may need to run it 10,000 times to damp out OS scheduling and other overheads.

Both are, of course, terribly misleading. The run time for your program -- as a whole -- is not the sum of the function run-times. You can only use the numbers for relative comparisons. They are not absolute measurements that convey much meaning.

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • 3
    Why the /1000? The method time.clock() returns seconds as a floating point value. If you expected it to return milliseconds, this would make sense, however dividing by 1000 converts to kiloseconds, a unit I have never seen used before. – pixelgrease Aug 15 '14 at 20:25
  • @pixelgrease milli / 1000 = micro, not kilo :) – stenci Oct 22 '14 at 20:06
  • @stenci He's suggesting that the resulting value is in seconds, e.g 1000 seconds. If you divide it by 1000, you get 1 "kiloseconds". – AturSams Jun 14 '15 at 15:58
  • 8
    @pixelgrease, /1000 because the function was executed 1000 times. So `time` is the average time it takes to execute the function once. – Mark Rajcok Jun 30 '15 at 18:05
  • I would propose not to overwrite the module name 'time' with a variable called 'time' – Dusch Jan 29 '19 at 18:08
15

If you want to time a python method even if block you measure may throw, one good approach is to use with statement. Define some Timer class as

import time

class Timer:    
    def __enter__(self):
        self.start = time.clock()
        return self

    def __exit__(self, *args):
        self.end = time.clock()
        self.interval = self.end - self.start

Then you may want to time a connection method that may throw. Use

import httplib

with Timer() as t:
    conn = httplib.HTTPConnection('google.com')
    conn.request('GET', '/')

print('Request took %.03f sec.' % t.interval)

__exit()__ method will be called even if the connection request thows. More precisely, you'd have you use try finally to see the result in case it throws, as with

try:
    with Timer() as t:
        conn = httplib.HTTPConnection('google.com')
        conn.request('GET', '/')
finally:
    print('Request took %.03f sec.' % t.interval)

More details here.

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
kiriloff
  • 25,609
  • 37
  • 148
  • 229
7

This is neater

from contextlib import contextmanager

import time
@contextmanager
def timeblock(label):
    start = time.clock()
    try:
        yield
    finally:
        end = time.clock()
        print ('{} : {}'.format(label, end - start))



with timeblock("just a test"):
            print "yippee"
JasonEdinburgh
  • 669
  • 1
  • 10
  • 17
  • Nice answer, simple and so far the only one I've found that allows you to send a label to the timer function. – Gabriel Dec 16 '14 at 14:43
  • Neat solution but a bit overly complicated. Spent an hour trying to figure out why `time.sleep(10)` only took 0.002 secs to execute according to this code. (btw, big difference between `time.clock()` and `time.time()` in python) – swdev Apr 13 '17 at 01:12
5

Similar to @AlexMartelli's answer

import timeit
timeit.timeit(fun, number=10000)

can do the trick.

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Binu Jasim
  • 297
  • 3
  • 9