Python Timeit() with Examples

What is Python Timeit()?

Python timeit() is a method in Python library to measure the execution time taken by the given code snippet. The Python library runs the code statement 1 million times and provides the minimum time taken from the given set of code snippets. Python timeit() is a useful method that helps in checking the performance of the code.

Syntax:

timeit.timeit(stmt, setup,timer, number)

Parameters

To work with timeit(), we need to import the module, as shown below:

import timeit

First Example

Here is a simple example of timeit() function

Code Example 1:

# testing timeit()
import timeit
print(timeit.timeit('output = 10*5'))

Output:

0.06127880399999999

We have seen a simple example that gives us the execution time of the simple code statement output = 10*5, and the time is taken to execute it is 0.06127880399999999.

Timing Multiple lines in python code

There are two you can execute multiple lines of code in timeit.timeit(), using a semicolon or by saving the code enclosed as a string with triple quotes.

Here are examples that show the working of it.

Example 1: Using semicolon

import timeit
print("The time taken is ",timeit.timeit(stmt='a=10;b=10;sum=a+b'))

Output:

The time taken is  0.137031482

Example 2: Using triple quotes

import timeit
import_module = "import random"
testcode = ''' 
def test(): 
    return random.randint(10, 100)
'''
print(timeit.repeat(stmt=testcode, setup=import_module))

Output:

C:\pythontest>python testtimeit.py
The time taken is  0.182619178

timeit - Methods:

Here, are 2 important timeit methods

timeit.default_timer() : This will return the default time when executed.

timeit.repeat(stmt, setup, timer, repeat, number) : same as timeit() , but with repeat the timeit() is called the number of times repeat is given.

Program Example 1:

# testing timeit()
import timeit
import_module = "import random"
testcode = ''' 
def test(): 
    return random.randint(10, 100)
'''
print(timeit.timeit(stmt=testcode, setup=import_module))

Output:

0.46715912400000004

Example 2:

default_timer() Example

# testing timeit()
 
import timeit
import random
 
def test(): 
    return random.randint(10, 100)
 
starttime = timeit.default_timer()
print("The start time is :",starttime)
test()
print("The time difference is :", timeit.default_timer() - starttime)

Output:

The start time is : 0.220261875
The time difference is : 0.0004737320000000045

Example 3: timeit.repeat()

# testing timeit()
import timeit
import_module = "import random"
testcode = ''' 
def test(): 
    return random.randint(10, 100)
'''
print(timeit.repeat(stmt=testcode, setup=import_module, repeat=5))

Output:

 [0.43638873, 0.5040939680000001, 0.5069179909999999, 0.3943449330000002, 0.3546886979999999]

timeit.repeat() works similar to timeit.timeit() function, with the only difference it takes in the repeat argument and gives back the execution time in array format with values as per the repeat number.

Executing timing function timeit.timeit() inside command-line interface

The syntax to execute your function inside timeit() on the command line is as follows:

python -m timeit [-n N] [-r N] [-s S] [-t] [-c] [-h] [code statement ...]

Command line parameters:

Example:

C:\pythontest>python -m timeit -s 'text="hello world"'
20000000 loops, best of 5: 13.1 nsec per loop

Another way you can execute inside command line is as shown below:

Example :

>>> import timeit
>>> print("The time taken is ",timeit.timeit(stmt='a=10;b=10;sum=a+b'))
The time taken is  0.15048536300000137
>>>

Why is timeit() the best way to measure the execution time of Python code?

Here are a few reasons why we consider timeit() is the best way to measure execution time.

Summary

Timeit()is used to get the execution time taken for the small code given

Parameters used with timeit()

 

YOU MIGHT LIKE: