Introduction to Python Timeit
In this article, we will discuss the python built-in library for calculating the execution time of the python program such a library in Python provides a method known as timeit() method. We can also calculate the execution time by calculating the start time and end time of the execution of the program and then subtract them where this process is very time consuming and hence we use timeit()method to do so and this method is also useful for calculating the program performance. The timeit will run the python program million times to get an accurate measurement of the code execution.
Working of timeit() Method with Example
This basic function is provided by the timeit library of Python for calculating the execution time of small Python code snippets. To calculate the accurate time of execution of the program by running the timeit method one million of time which is the default value.
The timeit module provides many different functions and the most important functions are timeit() which is for calculating the time of execution of program, repeat() function can be used with timeit() to call this timeit() function the number of times in the repeat is specified, and default_timer() this function is used to return the default time of the execution of the program. Let us demonstrate these functions in the below examples along with syntax.
Timeit(): This is a very important and useful function provided by the timeitmodule to calculate the execution of a given python code. This timeit() function can be executed even in command-line interface Let us see syntax and example in the below section.
Let us see the syntax of timeit() function which accepts four parameters and it is given as:
timeit.timeit(stmt, setup, timer, number)
- stmt: This parameter is to measure the statement which you want and the default value is “pass”.
- setup: This parameter is used to set the code that must be run before the stmt parameter and this also has a default value as “pass” and this parameter is mostly used for importing other code modules that are required for this code.
- timer: This parameter is timeit object and it has its default value.
- number: This parameter is used to specify the number which is used to mention how many executions you wish to run the stmt parameter. The default value of this parameter is 1 million (1000000)
- The return value of this function timeit.timit() is in seconds which returns the value it took to execute the code snippets in seconds value.
Now we will see a simple example that uses timeit() function. The timeit() function uses parameter stmt which is used to specify the code snippets which we want to calculate the time of execution of the given code snippets in stmt. In this article, we can have stmt in a single line using single quotes or multiple lines using semicolon or triple quotes.
Example of Python Timeit
Below is the example of Python Timeit:
import timeit setup_arg = "import math" print("Program to demonstrate the timeit() function for single and multiline code") print("\n") print("The single line code in stmt parameter is ") stmt1 = 'result = 9 * 6' print(stmt1) print("\n") print("The multiple code statement using semicolons is given as follows:") stmt2 = 'p = 9;q = 6; product = p*q' print(stmt2) print("\n") print("The multiple code statement using triple quotes is given as follows:") stmt3 = ''' def area_circle(): r = 3 res = math.pi * r * r ''' print(stmt3) print("\n") print("The time taken to execute the above code statement 1 which uses semicolon is:") print(timeit.timeit(stmt1, number = 1000000), 'seconds') print("\n") print("The time taken to execute the above code statement 2 which uses semicolon is:") print(timeit.timeit(stmt2, number = 1000000), 'seconds') print("\n") print("The time taken to execute the above code statement 3 which uses trile quotes is:") print(timeit.timeit(setup= setup_arg, stmt = stmt3, number = 1000000), 'seconds') print("\n")
In the above program, we saw how we used setup, stmt, and number arguments along with the statements which have one or more than one line of code in it. To write more than one line of code we have used a semicolon in stmt2 and we can also use triple quotes for writing multiple lines and we have written it in stmt3. Therefore the import statement required for stmt3 is assigned to the setup parameter of the timeit() function as this is used to execute the stmt value of the function and then it returns the time of the execution of statements after running number = 1000000 times.
In Python, timeitmodule provides timeit() function for calculating the execution time. Before using this function we should note some points we cannot use this function everywhere that means this is used only for small code snippets. It is better to use some other functions instead because timeit() function calculates the execution of code in seconds but what if the code is taking few minutes to execute then this function will not be recommended. As we discussed above the time module used for calculating the execution of the program is not recommendable because it will also take some background time which may not give accurate time. This timeit() function has both command line interface as well as callable function.
In this article, we conclude that timeit module in Python provides timeit() function. This function is used for calculating the execution time of the given code statements. In this article, we saw a basic example and also we saw how the statements can be written using semicolons or triple quotes for single line statements and multiple lines of program statement. We also saw examples for writing examples with timeit() function arguments such as setup, stmt, and number. This timeit() function is very useful than the time module as this will help to calculate the performance of the code easily and quickly. This timeit() function must be used only for the small code snippets as this is not much recommendable for lengthy code snippets as it returns the time in seconds.
This is a guide to Python Timeit. Here we also discuss the introduction and working of python timeit along with a different example and its code implementation. You may also have a look at the following articles to learn more –