This module provides an easy way to find the execution time of small bits of Python code.
Why time?
So now let's start exploring this handy library!
The module function timeit.timeit (stmt, setup, timer, number) takes four arguments:
Where timeit.timeit () returns the number of seconds it took to execute the code.
Example 1
Let's look at a basic example first.

Note: Note output is the time it takes to iterate the number times of the code snippet, and more than one iteration. For one iteration of exec. time, divide the exit time by number .
Example 2
Let's look at another practical example in which we compare two search methods, namely: binary search and linear search .
In addition, here I will demonstrate two more features: the timeit.repeat function and calling functions already defined in our program.

from __main__ import binary_search from random import randint
This will import the definition of the binary_search function already defined in the program , and a random library function randint .
Example 3
Finally, below I will demonstrate how you can use the command line interface of the timeit module:
Here I explain each term separately:
So, this was a short but short introduction to the timeit module and its practical application.
This is a very handy tool for Python programmers when they need a quick glance at the execution time of their code snippets.
This article is provided by Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.