avatarYang Zhou

Summary

The article outlines five methods for measuring the execution time of Python code, catering to different use cases and preferences for Python developers.

Abstract

The article "5 Ways To Calculate the Execution Time of Your Python Programs" introduces Python developers to various techniques for assessing code performance. It emphasizes the importance of evaluating execution speed beyond just ensuring a program is bug-free. The methods include using system commands like Measure-Command in Windows PowerShell and time in Linux Shell for script evaluation, IPython's timeit magic command for data scientists, the timeit module for precise measurement of small code snippets, the time module for general-purpose timing, and creating a custom decorator for logging function execution time. Each method is detailed with examples and best practices, aiming to help developers choose the most suitable approach for their specific needs.

Opinions

  • The author suggests that a bug-free Python program may not necessarily be optimized for speed, implying that performance evaluation is a crucial step in development.
  • For data scientists, the use of IPython's timeit magic command is recommended as a convenient tool within their familiar environment.
  • The timeit module is presented as a versatile tool for those not using IPython, with the ability to run directly from the command line or within a Python script.
  • The time module is highlighted for its flexibility in measuring execution time, with different functions recommended based on the desired level of precision and context (e.g., time.perf_counter() for high-resolution timing).
  • The author expresses that having time.perf_counter() calls scattered throughout the code can be cumbersome, advocating for the use of a decorator to streamline the process of logging execution time.
  • The article concludes with an encouragement for readers to support the author by joining Medium through their referral link, indicating a personal stake in the platform's membership model.

Python

5 Ways To Calculate the Execution Time of Your Python Programs

How fast is your Python code?

Image from Wallhaven

So, you wrote an elegant Python program and it worked as expected.

Congratulations! 🎉

But to be fair, it’s just the first step. 🙂

A bug-free program is not necessarily fast enough.

In reality, a Python developer should know a few tricks to evaluate the execution time of code. It will be super helpful, especially when comparing the performance of different algorithms or implementations.

This article will introduce 5 commonly-used methods to calculate the execution time of Python programs. After reading, it will be just a cup of tea to choose the proper one for your requirements. 🧋

1. Using the System Commands: Evaluating a Python Script’s Execution Time

Every operating system has its own command to calculate a program’s execution time.

Let’s say there is a simple Python file named “test.py”.

In Windows PowerShell, we can use the Measure-Command command to evaluate the execution time of it:

In Linux Shell, such as bash, the similar command is time, so the whole evaluation command should be:

time python test.py

However, it’s not necessarily that we always would like to evaluate a whole Python script.

Here are more options for more precise calculations.

2. Using the Magic Command of IPython: A Good Choice for Data Scientists

If you are a data scientist, you are probably using Jupyter Notebook or DataSpell. These tools use a common interactive Python shell — IPython.

IPython provides us with many convenient magic commands. One of them is timeit command.

Based on your using scenario, you can use it as a line-level command or cell-level command.

IPython line magic to measure execution time

As the above example shows, we can use %timeit to measure the execution time of one line of Python code.

IPython cell magic to measure execution time

As the above instance demonstrated, we can use %%timeit to measure the execution time of a cell of Python code in IPython.

3. Using the timeit Module: Measuring the Execution Time of Small Python Snippets

If you’re not using IPython, no worries at all. There is a Python built-in module named timeit which provides a similar way to time small bits of Python code.

You can run it directly under a command line interface:

python -m timeit 'for i in range(10000000):pass'
# 2 loops, best of 5: 117 msec per loop

Or you can call it inside your Python programs:

import timeit

print(timeit.timeit('for i in range(10000000):pass', number=1))
# 0.1310928

For more complex situations, there are 3 arguments you need to consider when using the timeit.timeit() function:

  • stmt: The code snippet you want to measure. Its default value is pass.
  • setup: To execute some prerequisites before running the stmt. Its default value is also pass.
  • number: The number of executions you’d like to run the stmt.

Let’s see a more complex example:

import timeit

# prerequisites before running the stmt
my_setup = "from math import sqrt"

# code snippet we would like to measure
my_code = '''
def my_function():
    for x in range(10000000):
        sqrt(x)
'''

print(timeit.timeit(setup=my_setup,
                    stmt=my_code,
                    number=1000))
# 6.260000000000293e-05

4. Using the time Module: Calculate the Execution Time as You Need

Python has another module called time that is also helpful for calculating the execution time of our code.

Basically, the idea is to capture the start and end timestamps before and after running a piece of code and then get the time difference:

import time

def my_function():
    for i in range(10000000):
        pass
start = time.perf_counter()
my_function()
print(time.perf_counter()-start)
# 0.1179838

The above example uses the time.perf_counter() to get the time. There are a few other options in the time module.

Therefore, a common question is which timer you should choose from the time module to calculate the execution.

Based on the specifics of every timer, the best practice is:

  • If you need to print the current time, use time.timer().
  • If you need to calculate the execution time of a program, use time.monotonic() (low resolution) or time.perf_counter() (high resolution)
  • If you need to precisely calculate the CPU time of a process, use time.process_time()
  • If you need to precisely calculate the CPU time of a thread, use time.thread_time()

The following article will give you more in-depth explanations about them:

5. Writing a Decorator for Logging the Execution Time of a Python Function

It’s kind of annoying if the time.perf_counter() functions are everywhere on your programs.

If logging a function’s execution time is a common functionality of your software, you can make a Python decorator to do this:

import time


def log_execution_time(func):
    def wrapper(*args, **kwargs):
        start = time.perf_counter()
        res = func(*args, **kwargs)
        end = time.perf_counter()
        print(f'The execution of {func.__name__} used {end - start} seconds.')
        return res

    return wrapper


@log_execution_time
def my_function():
    for i in range(10000000):
        pass


my_function()
# The execution of my_function used 0.1156899 seconds.

As the above code shows, a decorator can make your code more elegant and clean. You can just add the log_execution_time decorator to the function that needs to calculate the elapsed time of its execution.

Thanks for reading. ❤️

Join Medium through my referral link to access millions of great articles and support me.

Programming
Python
Data Science
Software Development
Technology
Recommended from ReadMedium