A Beginners Guide to Time profiling in Python.

Ankur Ranjan
Analytics Vidhya
Published in
3 min readJun 27, 2020

Writing a python code gives us great power to showcase our idea by easily programming them but like someone has rightly said “With great power comes great responsibility”. As python is a high level interpeter based language, there is always a need to check the time complexity and code profiling. This blog series will mainly focus on using ipython magic methods to do the same.

Part 1). Using %timeit for profiling.

While writing a simple block of code in python we usually get stuck for a moment when we need to choose one code out of the many alternatives. For example, we need to declare a variable and assign it to an empty list then we can either define it as variable = list() or variable = []. Let’s try to understand one more example before we start time profiling our code.

Now, let's assume we need to create a list containing numbers 0–50. For that we could use something like:

new_list = list()
for num in range(0,51):
new_list.append(num)
******************or********************new_list = [num for num in range(0,51)]******************or********************new_list = [*range(0,51)]

Now the question is: Which code is most efficient in terms of time required?

There are some ipython magic methods which are quite handy to use for such problems. Those who don’t know the ipython, can refer to this link. We can calculate runtime with IPython magic command %timeit.

Let’s see some examples of using %timeit, then we will understand this magic method.

The output shows, `mean and standard deviation` when the block of code was run for 7 times for 10000000 loops. As you can see, when we create an empty list using [] then it actually takes less time than creating it by using list() but you should understand the mutable nature of the list before using [] or list(). You can specify the number of runs/loops by setting the flags ( -r )/( -n) respectively.

For time profiling multiple lines of code, we can use %%timeit. Let’s use this to check the time taken for the code below.

It is pretty evident that list comprehension is much better than writing a simple for loop. However, unpacking range object by using an asterisk (*) is much better than using list comprehension for the above problem.

Yahoo!!! We learned something new today in the context of time profiling. Let’s meet in the next part of this series and learn some more magic methods for time profiling in python. Till then goodbye and keep learning.

Please Find Part 2 here.

--

--

Ankur Ranjan
Analytics Vidhya

Data Engineer III @Walmart | Contributor of The Big Data Show