Start Using The timeit Library in Python!

Introduction The purpose of this article is to introduce us to the timeit library.

Suppose you want to measure the execution time of a code snippet.

What do you do? Till now, I just like most people would do something like this: import time start_time = time.

time() “””Some Code””” end_time = time.

time() print(f”The execution time is: {end_time-start_time}”) Now say we want to compare the execution times of two different functions, then: import time def function_1(*parameters): #do something # return value def function_2(*parameters): #do something #return value #measuring time taken by function_1 start_of_f1 = time.

time() value1 = function_1(*parameters) end_of_f1 = time.

time() print(f”The time taken by function_1 is:{start_of_f1-end_of_f1}”)#measuring time teaken by function_2 start_of_f2 = time.

time() value2 = function_1(*parameters) end_of_f2 = time.

time()print(f”The time taken by function_1 is:{start_of_f2-end_of_f2}”) This is the way most people measure execution times and compare two functions.

However, if we have to measure the time taken by a very small code snippet, this method fails to produce accurate results.

Even, while comparing the functions, all the other processes and tasks happening in the OS are messing around with the time.

time() instance.

So it is not accurate while comparing functions.

To address this problem, Python has a timeit module that is meant to measure the execution times of small and large code snippets.

Here in this article, we first take a look at the timeit library in python using some examples.

First, let us look at its overview as provided in the documentation.

timeit in Python This module provides a simple way to time small bits of Python code.

It has both a Command-Line Interface as well as a callable one.

It avoids a number of common traps for measuring execution times.

This module has a function, timeit.

timeit(stmt = pass, setup= pass, timer = <default timer> ,number= 1000000) .

This function takes four arguments: stmt: The code snippet whose execution times is to be measured.

setup: The code to run before executing stmt.

Generally, it is used to import some modules or declare some necessary variables.

It will become more clear with the examples.

timer: It is a default timeit.

Timer object.

It has a sensible default value to it, so we do not have to do much about it.

number: It specifies the number of times stmt will be executed.

It will return the time taken (in seconds) to execute the stmt, the specified number of times.

Let us look at some examples to understand it better.

import timeit #This code will be executed only once, before stmt.

setup_code = “from math import sqrt” #This code will be executed as specified by the parameter number stmt_code = “sum(sqrt(x) for x in range(1,10000))” iterations = 10000 time = timeit.

timeit(stmt = stmt_code, setup = setup_code, number = iterations) print(time) #17.


print(f”The time for single iteration is {time/iterations}”) All we did was to pass the code as a string and specify the number of iterations to the timeit.

timeit function.

Now let us look at a way to use this amazing library to compare two functions.

For this example, let us compare two sorting functions, one is a simple bubble sort, and the other is the Python’s built-in sorted() function.

Here we will also see another timeit function called repeat().

def bubble_sort(arr): for i in range(len(arr)): for j in range(0, len(arr) – i – 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr def make_random_array(): from random import randint return [randint(-100, 100) for i in range(20)] test_array = make_random_array() Since we want to make sure that we test our functions on the same array, we are declaring an array here and will later call it in compare_functions().

def compare_functions(): import timeit setup_code1 = “”” from __main__ import bubble_sort from __main__ import test_array “”” setup_code2 = “”” from __main__ import test_array “”” stmt_code1 = “bubble_sort(test_array)” stmt_code2 = “sorted(test_array)” times1 = timeit.

repeat(stmt=stmt_code1, setup=setup_code1, number=10000, repeat=5) times2 = timeit.

repeat(stmt=stmt_code2, setup=setup_code2, number=10000, repeat=5) print(f”Time taken by bubble sort is {min(times1)}”) print(f”Time taken by built in sort is {min(times2)}”) if __name__ == “__main__”: compare_functions() “”” Time taken by bubble sort is 0.

2646727000001192 Time taken by built in sort is 0.

0031973000000107277 “”” Here, the output is the minimum value in the list times1 and times2.

In the setup code, we had, from __main__ import bubble sort from __main__ import arrThis imports the bubble_sort function already defined in our program and also import the array test_array which we declared in the program earlier.

This will make sure that both the functions are tested on the same array, test_array.


repeat() takes an additional parameter, repeat (default value = 5).

This returns a list of execution times of the code snippet repeated a number of times.

As expected, the time taken by bubble sort is much more than the built-in sorted() function.

Now to wrap up, let us look at how we can use the command-line interface in this library.

When called as a program from the command line, the following form is used: python -m timeit [-n N] [-r N] [-u U] [-s S][statement .

] Here, -n N, How many times to execute the ‘statement.

’ -r N, How many times to repeat -s s, setup statement -u u, unit of timer output, (nano, micro, mil, sec) Some examples as are: $ python -m timeit -s text = “sample string”; char = “g” char in text 5000000 loops, best of 5: 0.

0877 usec per loop $ python -m timeit -s text = “sample string”; char = “g” text.

find(char) 1000000 loops, best of 5: 0.

342 usec per loop This wraps up the introduction of the timeit module.

I tried to cover all one needs to know to start using this module from now on.

Here is the official documentation of the module.

Please take a look at it to know more about this module.

I would also encourage you to take a look at the source code of this module here.

I hope you learned something from this article, and from now on, you can use this module’s functionality in your code.


About the Author Kushagra Bansal I am an undergraduate student majoring in Mathematics and I am a passionate learner with keen interests in Computer Science and Astrophysics.

I identify myself as a quick learner and believe in learning by practicing.

New technologies like Machine learning, Deep learning, and computer vision, etc impress me a lot.

I also like to read a lot.

  You can also read this article on our Mobile APP Related Articles (adsbygoogle = window.

adsbygoogle || []).


Leave a Reply