How to check execution time of Python scripts ?

Aparna Mishra
2 min readOct 10, 2021

When developing code and working on data preprocessing steps, it is important to compare various methods and optimize our code. This can be done by first understanding which section of the code is taking long time to execute. These are small steps but they play a very significant part as they give us insight about which method is better and takes minimum time to run.

This article will show some methods in which we can do time profiling.

Method 1:

import timestart = time.time()
sum = 0
for i in range(1000):
for j in range(1000):
sum += i**j
end = time.time()
print('time for execution:',end-start)

Output:

time for execution: 5.5152599811553955

Method 2 : %timeit

%timeit can also be directly used from command line interface, here I am running it on Python Interface.

import timeit

Let’s check the time calculated for adding numbers in range 200.

%timeit sum(range(200))

Output:

1.2 µs ± 7.49 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

The output shows mean and standard deviation when the code runs 7 times for 1000000 loops. We can also specify the number of runs and loops by setting up flags {-r} and {-n}. Below snippet explains how.

%timeit -r3 -n5 sum(range(100))

Output:

1.05 µs ± 73.6 ns per loop (mean ± std. dev. of 3 runs, 5 loops each)

Let’s check different methods of creating a dictionary and find out the best one after checking it’s time for execution.

#method A for creating dictionary-------
%timeit -r3 -n3 items1 = {"pen" : 7,"paper" : 10,"sketchpens" : 45,"apples" : 23,"stickers" : 77 }
#method Bfor creating dictionary-------
%timeit -r3 -n3 items2 = dict(pen = 7,paper = 10,sketchpens = 45,apples = 23,stickers= 77)

Output:

211 ns ± 87.5 ns per loop (mean ± std. dev. of 3 runs, 3 loops each)333 ns ± 94.3 ns per loop (mean ± std. dev. of 3 runs, 3 loops each)

Clearly, the method B takes longer time than method A. This helped us in selecting the best way to create a dictionary.

Method 3: %prun

This returns a pager with details about where the execution is spending the most time which can be used to optimize our algorithm.

type %prun? to know more.

def sum_of_lists(N):
total = 0
for i in range(5):
L = [j**3 for j in range(N)]
total += sum(L)
return total

Checking time.

%prun sum_of_lists(1000000)

Conclusion:

These are just few examples of how execution time can be calculated using these 3 methods.

Here are a few resources that you can refer.

  1. timeit — Measure execution time of small code snippets — Python 3.10.0 documentation
  2. Profiling and Timing Code | Python Data Science Handbook (jakevdp.github.io)

Hope this was helpful!!

--

--