Home »
Python
timeit() Function with Example in Python
Python | timeit() function: In this tutorial, we are going to learn about the timeit() function with example.
Submitted by Gopeesh Upadhyay, on April 02, 2020
Today it is very important to finish a project not only in limited resources but it is also important to finish it under the least possible period. We have time() function in Python to calculate the time of execution of code.
When we execute any code several background operations happen to execute our code execution. When time() function counts on the time it does not take into consideration background operations going on to execute this code. Also, we subtract time in starting and at the end to get the required time. Now timeit() function has been developed to find exact timing taken any particular code execution. Even it counts on timing up to .0000 decimal points of a millisecond to find exact timing taken in cod execution. Here we will see how this function works.
Code Execution for timeit() function through an example:
We will type python and hit enter. You will respond like the version of python and Microsoft we are using. Also, it confirms that we are working in python.
Import statement:
import timeit
If you will get a response like 'import' is not defined then go and check for system variable has been updated or not.
Now, we will program a simple mathematical operation and find its execution timing by two methods.
# python example to demonstrate the
# example of timeit() function
# importing the module
import timeit
# operations and time calcuation
print('a' *5)
print('a' + 'a' +'a' + 'a' + 'a' )
print(timeit.timeit("b = 'a' *3",number = 10000000))
Output
aaaaa
aaaaa
0.9088429469993571
Here you can put the value of number any number times as number times code execution required.
import timeit
print(timeit.timeit("ab = 'a' + 'a' + 'a' + 'a' + 'a' ", number=10000000))
Output
0.8887228329986101
This method describes the method to find execution timing manually. Manually here means we have to perform the whole operation a number of times.
Automatic Repetition Method with Example
Now we will see how to perform the same tedious operation automatically?
import timeit
print(timeit.repeat("b = 'a'* 3", number = 10000000,repeat = 5))
print()
print(timeit.repeat("ab = 'a' + 'a' + 'a' + 'a' + 'a' " , number = 10000000,repeat = 5))
Output
[0.8862817650006036, 0.8274680489994353, 0.891247380997811, 0.9186934919998748, 0.900538891000906]
[0.8854398910043528, 0.8779188379994594, 0.8196939810004551, 0.8731913320007152, 0.8250786080025136]
Here we can see repetitions happen a number of times automatically.
Why timeit()?
timeit() function has been the best option to find execution timing than time function because of its accuracy. Many of our assumptions regarding code execution in time() function were wrong which is rectified in timeit() function like of consideration of time taken by background operations also. Similarly, timeit() is also a more precious and shortest path to find execution timing.