Thu 28 May 2015

Filed under Python


A great library for testing the time of short code (a few short lines).

The syntax:

import timeit
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000)

timeit.repeat(stmt='pass', setup='pass', timer=<default timer>, repeat=3, number=1000000)
- `stmt`: this is the statement that is being timed. It is a **string**; it is enclosed by quotes and can include newlines.  This statement can be run or repeated a number of times.
- `setup`: this is the setup statement - to setup variables for example.  The setup is run only once - at setup.   
- `timer`: usually the default timer for the platform you are using (Windows or Unix)  
- `repeat`: how many times should the statement be repeated in this test. All of the responses are returned in a list.
- `number`: controls how many times to run the statement; the default is 1,000,000.

*Note: You don't really need to include the keywords of the positional arguments like stmt and setup. You do need `repeat` or `number`.*

Some examples:

import timeit

setup = "c= {'Quiz1': 90, 'Quiz2':91, 'Test1':89, 'Quiz3':87, 'Test2':90, 'Quiz4':89, 'Final':88}"

stmt = """c['Final']=c['Final'] + 3 """

timeit.repeat(stmt=stmt, setup=setup, repeat=10, number=1)

This is testing how long it will take to add 3 points to the Final mark.
- First the setup, written as a string: the initial marks are set up in a dictionary; each quiz or test is a keyword.
- Then the statement, written as a string. This adds 3 to the Final mark. - Lastly, the timeit. We are repeating this 10 times - so we can see the various times it takes to run this code; it is returned as a list.

[0.002605915069580078, 0.0019431114196777344, 0.0020720958709716797, 0.0030710697174072266, 0.0030739307403564453, 0.00308990478515625, 0.0024268627166748047
, 0.002237081527709961, 0.0023369789123535156, 0.0021729469299316406]

Let's compare a 2 methods that return the same results:

import timeit

setup = "list = [47, 11, 42, 102, 13]"
stmt1 = """max(list)"""
stmt2 = """reduce((lambda a, b: a if a > b else b), list)"""

test_max = timeit.repeat(stmt=stmt1, setup=setup, repeat=5, number=1000)
test_reduce = timeit.repeat(stmt=stmt2, setup=setup, repeat=5, number=1000)

print 'Max {}.format(test_max)
print 'Reduce {}.format(test_reduce)

The results:

Max [0.00048422813415527344, 0.0004940032958984375, 0.0005421638488769531, 0.0008389949798583984, 0.0008420944213867188]

Reduce [0.0017399787902832031, 0.001605987548828125, 0.0016090869903564453, 0.001589059829711914, 0.0015668869018554688]

You can use timeit with a function too

- First define your function, as normally.  
- Then in the timeit code, 
1. include the function in quotes.
2. add `setup = "from __main__ import funcname"`. Notice the double underscores around the word main `__main__`
def add(x,y):
    return x + y

import timeit
 timeit.repeat("add(4,5)", "from __main__ import add", repeat=3, number=1000)

More about Timeit

The docs on Timeit From timeit from Python Module of the Week


DeeKras.com © Dee Kras Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More