How to measure the execution time of a Python script Interactive Course


Let’s say you want to know the execution time of the following Python code:

a = range(100000) 
b = [] 
for i in a:     
    b.append(i*2)

 

There are a few ways to measure the time it takes for a Python script to execute, but here’s the best way to do it and I will explain why:

import timeit
code_to_test = """
a = range(100000)
b = []
for i in a:
    b.append(i*2)
"""
elapsed_time = timeit.timeit(code_to_test, number=100)/100
print(elapsed_time)

 Output: 0.01137321546

That’s the output I get on my Macbook Pro. So, that’s more or less 1/100 of a second.


How the above script works:

Line 1: We import the timeit module.

Line 3: We create a variable. In that variable, we are storing the code we want to test. This code has to go inside triple quotes. So, the test code is provided as a string.

Line 10: We call the time.timeit() function. The timeit() function will get the test code as an argument, executes it and records the execution time. To get an accurate time, I ordered timeit() to perform 100 cycles. Therefore, I had to divide the output by 100 to get the execution time for only one cycle.

Line 11: We simply print out the execution time. The result is the execution time in seconds.


Why is timeit() the best way to measure the execution time of Python code?

1. You can also use time.clock() if you are on Windows and time.time() if you are on Mac or Linux instead of using timeit(). However, timeit() will automatically use either time.clock() or time.time() for you in the background, depending on what operating system you have to give you the most accurate results.

2. timeit() disables the garbage collector, which could otherwise skew the results.

3.timeit() repeats the test many times (100 times in our case) to minimize the influence of other tasks running on your operating system.


Exercise:

By the way, the code we tested above builds a list by multiplying the items of another list. I can achieve the same result using a list comprehension:

a = range(100000)
b = [i*2 for i in a] 

If you have nothing else to do and fancy an exercise, try measuring the execution time of the above code using timeit() to see if a list comprehension is better than a for loop.

Lastly, a tip: Close heavy programs that are running on your computer when you do such tests, so you get even more accurate results that are not affected by heavy processor tasks.


Do you struggle to create real-world applications with Python?

You can start learning Python the fun way by building 10 amazing Python apps by buying my premium Udemy course The Python Mega Course: Build 10 Real-World Applications. The Python Mega Course is the most practical course you will find on the web nowadays. Over 100 thousand students so far have used the course to learn Python programming and to build real-world applications in Python 3.


If you want to learn more about the course, and take a look at its curriculum, just click here. The course follows a modern-teaching approach where students learn by doing. Start Python from the basics and learn how to create 10 amazing and professional Python programs used in the real world! You will code the 10 apps, guided step-by-step by easy video explanations and continuous support by the course instructor. The applications you will build in the course consist of database apps, web apps, desktop apps, web scraping scripts, webcam object detectors, web maps, data visualization dashboards, and more. These programs are not only great examples to master Python, but you can also use them for your portfolio.

Next Lecture
Recommended Course

Learn Flask development and learn to build cool apps with our premium Python course on Udemy.


Mailing List

We will keep you Python-motivated if you subscribe below.