timeit() Function with Example in Python

natamacm

natamacm

Posted on June 9, 2020

timeit() Function with Example in Python

You will learn about the Python timeit() function and shown an example in this tutorial. You can use it to calculate the time of code execution.

If you run any code, our code execution takes place with a number of background operations. When time() function counts on the time it does not take into consideration background operations going on to execute this code.

time it

It subtracts time to get the necessary time at beginning and at the end. timeit() function has been developed to define the exact timing of each execution of the code.

This works both in the Python shell and from a script

timeit() example

Code execution time with timeit() function as an example:
We're going to type and hit python. It reacts with the python version you are using and it also confirms that you work in python.

Import statement:

    import timeit
Enter fullscreen mode Exit fullscreen mode

Note we're not using the time module but timeit module.

If you will get a response like 'import' is not defined then go and check for system variable has been updated or not.

The program does a simple mathematical operation and find its execution timing by two methods.

# python example of timeit() function

# load module
import timeit

# time calcuation
print('x' *5)
print('x' + 'x' +'x' + 'x' + 'x' )
print(timeit.timeit("y = 'x' *3",number = 10000000))
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

xxxxx
xxxxx
0.14592579402960837
Enter fullscreen mode Exit fullscreen mode

Here you can put the value of number any number times as number times codeexecution required.

import timeit
print(timeit.timeit("xy = 'x' + 'x' + 'x' + 'x' + 'x' ", number=10000000))
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

0.1387987380148843
Enter fullscreen mode Exit fullscreen mode

This method describes the method to find execution timing manually. If you want to check an operation many times, instead of a loop you can use the method below.

timeit example

Now you will see how to perform the same tedious operation automatically?

import timeit

print(timeit.repeat("y = 'x'* 3", number = 10000000,repeat = 5))
print()
print(timeit.repeat("xy = 'x' + 'x' + 'x' + 'x' + 'x' " , number = 10000000,repeat = 5))
Enter fullscreen mode Exit fullscreen mode

The program above outputs this:

[0.13703188695944846, 0.13094416703097522, 0.13312444603070617, 0.14463001501280814, 0.13111430197022855]

[0.1333333159564063, 0.1297284740721807, 0.12939571705646813, 0.1272417320869863, 0.12798011908307672]
Enter fullscreen mode Exit fullscreen mode

Here you can see repetitions happen a number of times automatically.

Why timeit()?

Because of its precision, the timeit() function is the best way of finding time execution than time.

Many of our assumptions with regard to execution of the code in timeit() function were incorrect and have been rectified in the timeit() function as well.

If you are new to Python, I recommend this Python course.

💖 💪 🙅 🚩
natamacm
natamacm

Posted on June 9, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related