Timeit library in Python:

In this article, we will be seeing a method of measuring the execution time of python code snippets. We will use a built-in function called timeit for this method. It is very simple to find out the execution time of tiny bits of Python code for Data Science.

Why we use timeit?
  • We can use a simple time module. We can just save time before and after the execution of the code and simply subtract them. This has a slight problem. It does not give precise results as some background programs running momentarily that disrupts the execution of code. This leads to significant variation in running time of small code snippets.
  • In order to get statistically significant measurement of code execution time, timeit runs the snippet of code millions of times( default value 1000000).
  • One major reason for using timeit is that it is simple to use and consists of a command line interface and a callable one.

The module function timeit.timeit(stmt, setup, timer, number) takes four arguments:

  • The argument stmt is the statement which we want to measure. It default to ‘pass’.
  • The argument setup is the code which is run before running stmt. It default to ‘pass’.
  • The timer is a Timer object. It has a sensible default value generally so, we don’t need to worry about it.
  • At last we have the number argument which defines the number of executions we would like to run the

When we use the timeit.timeit() function, it returns the number of seconds taken to execute the code.

First we will look at a basic example:

Timeit library in Python for Data Science - PST Analytics

  • For the above program we will get an output which will show the execution time (seconds) for 10000 iteration of the code snippet passed on to timeit function.
  • We should keep the import segment and other static pieces of code in setup argument.
  • Now we wil look into another example. In this we will compare two searching techniques, Binary and Linear.


Here we will also see time.repeat function and calling function already defined in the program.

Timeit library in Python for Data Science - PST Analytics


Timeit library in Python for Data Science - PST Analytics

The above output is the minimum value in the list times.

  • repeat function takes one extra argument, repeat. The output returned is a list of execution times of all the code runs which are repeated for a specified number of times.
  • In the setup argument we have passed,

The above imports the definition of function binary_search which is already defined in the program and the random library function randint.

  • It is seen that the execution time of binary search is lower than linear search.


Now we will demonstrate utilizing the command line interface of timeit module:

Timeit library in Python for Data Science - PST Analytics

Now each term is explained individually

So, to learn more about time functions in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.