timeit() Function with Example in Python
Tags: python
Published: 2020-06-09 14:08:04

You will learn about the <a href="https://python.org">Python</a> 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() <a href="https://pythonprogramminglanguage.com/functions/">function</a> counts on the time it does not take into consideration background operations going on to execute this code. 

![time it](https://dev-to-uploads.s3.amazonaws.com/i/leojpq115594m4kzmvsx.gif)

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 <a href="https://bsdnerds.org/learn-python/">shell</a> and from a <a href="https://pythonbasics.org/execute-python-scripts/">script</a>

## 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
    
Note we're not using the <a href="https://python-commandments.org/current-date-and-time-in-python/">time module</a> 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    
# 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))
```    

The program above outputs this:    
```    
xxxxx
xxxxx
0.14592579402960837
``` 
Here you can put the value of number any number times as number times codeexecution required.
   
```python    
import timeit
print(timeit.timeit("xy = 'x' + 'x' + 'x' + 'x' + 'x' ", number=10000000))
```    

The program above outputs this:    
    
    0.1387987380148843
   

This method describes the method to find execution timing manually. If you want to check an operation many times, instead of <a href="https://pythonspot.com/loops/">a loop</a> you can use the method below.

## timeit example

Now you will see how to perform the same tedious operation automatically?    
    
```python
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))
```    

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]
```    

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 <a href="https://gumroad.com/l/dcsp">this Python course</a>.