Measure execution time of small Python code snippets (timeit)


The Timer class and other convenience functions in timeit module of Python's standard library are designed to provide a mechanism to measure time taken by small bits of Python code to execute. The module has a command line interface and the functions can be called from within program as well.

Easiest way to measure time of execution is by using following convenience function

timeit()

This function returns object of Timer class. It mainly requires two parameters.

  • stmt − a string containing valid Python statement whose execution time is to be measured.

  • setup − a string containing Python statement which will be executed once, primarily to initialize certain objects or variables.

Both strings may contains multiple statements separated by semi colon(;) or newlines, and both default to pass keyword. Another optional parameter number may be given which denotes number of executions of 'stmt'

In following example time required to perform thousand times the cumulative addition of numbers in a range 0-100 is measured.

import timeit
setupcode = "s = 0"
function = '''
for x in range(100):
s = s + x
'''
print (timeit.timeit(setup = setupcode, stmt = function, number = 1000))

Here a string contains a for loop within which numbers within range 0-100 are added. This string is the stmt parameter. For addition, the initialization of a variable is done by a setupcode string. The timeit() function calculates the time required in seconds.

Output

0.03055878530880241

Timer class

The same result can be obtained by first creating a Timer object and then execute timeit() method on it.

repeat()

The Timer class also has a repeat() method to call timeit() repeatedly. It returns a list of all calls.

An object oriented version of the above code is as follows −

import timeit
setupcode = "s = 0"
function = '''
for x in range(100):
s = s + x
'''
t = timeit.Timer(setup = setupcode, stmt = function)
print (t.timeit(number = 1000))
print ('calling repeat() :',t.repeat(3,1000))

Output

0.019971274194651528
calling repeat() : [0.023369810546474253, 0.020518432391765262, 0.02075439436427058]

Command line interface

As mentioned earlier, timeit module has a command line interface. The module itself is imported using –m option in the command line for execution of Python script. Following command line options define various parameter line setup and executable code, repeat frequency, etc.

-n N, --number = Nhow many times to execute ‘statement’
-r N, --repeat = Nhow many times to repeat the timer (default 3)
-s S, --setup = Sstatement to be executed once initially (default pass)
-v, --verboseprint raw timing results; repeat for more digits precision
-h, --helpprint a short usage message and exit

Following is the command line equivalent of example code used earlier −

C:\Users\acer>python -m timeit -s "s = 0" "for x in range(100): s = s + x"
10000 loops, best of 3: 22.4 usec per loop

Updated on: 27-Jun-2020

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements