Python

How to Use the Python Timeit to Measure the Time of Code Snippets

When running a Python code snippet, it’s possible to check its execution time to optimize it. You can check the execution speed of the Python code to understand how to optimize and improve your code. Besides, if you are experiencing lagging or other bottlenecks, checking the execution time with the timeit() is helpful.

Throughout this post, we will cover the Python timeit(). We will discuss how to import and use the “timeit” module to check the execution time of code and give different examples on how to implement it. Moreover, we will see the different ways of implementing the timeit() to yield different results.

Working with the Python Timeit Module

Python offers the “timeit” module as a built-in module that you can import when executing your Python code and then specify the code snippet in which you want to check its execution time. When executed, the module runs the specified code snippets several times, with the default being 1000000 times, then give the minimum time taken as the execution time of your code snippet.

Here’s the syntax of the Python “timeit” module:

timeit.timeit(stmt, setup, timer, number)

In the previous syntax, the “stmt” stands for the statement that you wish to measure. If not included, the default value is “pass”. As for the setup, it refers to the code that runs before the “stmt” and defaults to “pass” when not included. The timer is the object that is used with the “timeit” module, while the number is the execution that you wish to run when you want to get the minimal time that is taken for the executions.

The default number of executions is 1000000, but you can specify another number. Let’s give examples of using the Python “timeit” module.

Example 1: Working with a Line of Code

The first example demonstrates how you import the Python “timeit” module and use it to get the execution time for a simple code snippet. Note that the code snippet must be enclosed as a string. Here, our code snippet is “test1”. From the image, you can note how we measured its execution time inside our “print” statement.

Example 2: Working with Multiple Lines of Code

Suppose you have numerous lines of code in your Python program and you want to measure their execution time. For that, enclosing them as strings won’t work. Instead, you have two options that you can use.

Option 1: Using the Semicolon

Here, you can separate the different lines using a semicolon inside the “timeit” module, and the lines of code are part of the “stmt” section of the module. The following is an example:

We still manage to get the execution time for the different lines of code.

Option 2: Using the Tripple Quotes

Alternatively, you can use the triple quotes instead of semicolons as shown in the following. Note that we specify the number of times as 100000 instead of going with the default option like in option 1.

Example 3: Working with Timeit Methods

There are two primary methods that you can use with “timeit” other than the default timeit.timeit().

1. default_timer()

It returns the default time. Here’s a simple example where we get the difference between the start and end time of when the code snippet executes:

2. repeat()

It executes similar to the timeit() but with the number of specified times. Here’s a simple example where we set the repeat as 4:

Conclusion

Using the Python “timeit” module is handy whenever you want to measure the execution time of your code snippet. We’ve seen what syntax to use and the given examples of utilizing the “timeit” module under different circumstances. Hopefully, you understood how the Python “timeout” module works.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.