When it comes to measuring the time of execution for a piece of Python code, we have many ideas. How about simply using the time module and saving the time before and after the execution of the program? This sounds easy and good but it isn’t.

Python timeit

When a program is executing, many background processes also run to make that code executable. The time module doesn’t take those processes into account. If you need precise time performance measurements, timeit is the module to go for.

The timeit module runs a piece of code 1 million times (default value) and takes into account the minimum amount of time it took to run that piece of code. Let’s see the module in action here!

Finding Execution time of Python code

There are many ways to use the timeit module. One of the easiest way is to use it directly on Python CLI. We will be doing this in our first example before moving into some more examples.

Python timeit Example

We will start by using the timeit module directly from the Python CLI. When CLI is used, we will notice that the module otself decides the number of repetitions performed for the same piece of code. Here is sample commands we executed for different expressions:

$ python -m timeit '"-".join(str(n) for n in range(100))'
$ python -m timeit '"-".join([str(n) for n in range(100)])'
$ python -m timeit '"-".join(map(str, range(100)))'

Let’s see the output for this program:

Finding time of execution from CLI


Finding time of execution from CLI

In some later sections, we will see how we can control the number of repetitions performed to find the best time for the execution of expressions.

Timing a piece of code

The best thing with the timeit module is that we can decide the exact code snippet for which we want to measure performance for. We will define the setup code and the code for performance test separately. The setup code is run just once whereas the main code is run 1 million times:

import timeit
# setup code is executed just once
mysetup = "from math import sqrt"
# main code snippet for performance check
def example():
    mylist = []
    for x in range(100):
# timeit statement
print(timeit.timeit(setup = mysetup,
                    stmt = mycode,
                    number = 10000))

Let’s see the output for this program:

Timing basic loop


Timing basic loop

With this code, we also demonstrated how we can control the number of times the code should be repeated for performance check.

Note that it is best to keep the import statements in setup code itself so that no alterations are made while executing the main code.

Record time for multi-line code from CLI

If making a script is not feasible for you and you want to quickly look at a code for its performance, using the code directly from CLI is also an option:

$ python3 -m timeit -s
> "jd = {}"
> "for idx in range(1000):"
> "  jd[str(idx)] = idx"

Let’s see the output for this program:

Timing multi-line code on CLI


Timing multi-line code on CLI

Generally comparing two blocks of code

If you only want really simple idea by comparing two pieces of code about how fast one run form another, there is a pretty simple way of doing this:

import timeit
start_time = timeit.default_timer()
print(timeit.default_timer() - start_time)
start_time = timeit.default_timer()
print(timeit.default_timer() - start_time)

Just use the default_timer() function to start the times and again to find a difference for it. This is the easiest way you can use the module to find the performance of code. This also forces you to make your code in the form of functions, which are modular entities.

Another way of doing the same thing, without touching the original script, say test.py which contains functions like:

def RadixSort(): ...
def TimSort(): ...

Use timeit module like this:

$ python -m timeit -s 'import test' 'test.RadixSort()'
$ python -m timeit -s 'import test' 'test.TimSort()'

Pretty easy, right?


In this lesson, we saw how we can measure the performance of small pieces of Python code using the timeit module using CLI and scripts as well.

By admin

Leave a Reply

%d bloggers like this: