timeit() method is available with python library timeit. It is used to get the execution time taken for the small code given. The library runs the code statement 1 million times and provides the minimum time taken from the set. It is a useful method that helps in checking the performance of the code.

Syntax:

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

Parameters

  • stmt: This will take the code for which you want to measure the execution time. The default value is "pass".
  • setup: This will have setup details that need to be executed before stmt. The default value is "pass."
  • timer: This will have the timer value, timeit() already has a default value set, and we can ignore it.
  • number: The stmt will execute as per the number is given here. The default value is 1000000.

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:

  • -n N: the number of times you want the code to execute.
  • -r N: the number of times you want the timeit() function to repeat
  • -s S: this will have setup details that will get executed before code execution.
  • -t: for this, you can make use of time.time()
  • -c: for this, you can make use of time.clock()
  • -h: for help
  • code statement: The code details.

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.

  • It runs the code statement 1 million times that is the default value, and from that, it will return you the minimum time taken. You can also increase/decrease the 1 million by setting the argument number in time () function.
  • While executing the test, the garbage collection is disabled every time by time () function.
  • timeit() internally takes the accurate time as per your operating system being used. For example, it will use time.clock() for Windows operating system and time.time() for mac and Linux.

Summary

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

Parameters used with timeit()

  • stmt: This will take the code you want to measure the execution time
  • setup: This will have setup details that need to be executed before stmt
  • timer: This will have the timer value, timeit() already has a default value set, and we can ignore it.
  • number: The stmt will execute as per the number is given here.

 

YOU MIGHT LIKE: