Introduction to Benchmarking
Explore the fundamentals of benchmarking in Python to measure code execution speed and identify bottlenecks. Learn how to use the timeit module both from the command line and within scripts to accurately time code snippets. This lesson prepares you to analyze code performance effectively for future optimization efforts.
We'll cover the following...
Overview of benchmarking
What does it mean to benchmark ones code? The main idea behind benchmarking or profiling is to figure out how fast our code executes and where the bottlenecks are. The main reason to do this sort of thing is for optimization. We will run into situations where we need our code to run faster because our business needs have been changed. When this happens, we will need to figure out what parts of our code are slowing it down.
This chapter will only cover how to profile our code using a variety of tools. It will not go into actually optimizing our code. Let’s get started!
timeit
Python comes with a module called timeit. We can use it to time small code snippets.
The timeit module uses platform-specific time
functions so that we will get the most accurate timings possible.
The timeit module has a command line interface, but it can also be
imported. We will start out by looking at how to use timeit from the
command line. Open up a terminal and try the following examples:
python -m timeit -s "\[ord(x) for x in 'abcdfghi'\]"
100000000 loops, best of 3: 0.0115 usec per loop
python -m timeit -s "\[chr(int(x)) for x in '123456789'\]"
100000000 loops, best of 3: 0.0119 usec per loop
What’s going on here? Well, when we call Python on the command line and pass it the -m option, we are telling it to look up a module and use it as the main program. The -s tells the timeit module to run setup
once. Then it runs the code for n number of times that is 3 times here and returns
the best average of the 3 runs. For these examples, we won’t see much difference.
Your output will likely be slightly different as it is dependent on your computer’s specifications.
Let’s write a function and see if we can time it from the command line. Try the above commands in the following terminal:
# simple_func.py
def my_function():
try:
1 / 0
except ZeroDivisionError:
passAll this function does is cause an error that is promptly ignored. To get timeit to run this code on the
command line, we will need to import the code into its namespace, so make sure we have changed our current working directory to be in the same folder that this script is in. Then run the following in the above terminal:
100000000 loops, best of 3: 0.0112 usec per loop
Here we get the output by the command, telling us the time taken by loop.
Importing timeit for testing
Using the timeit module inside our code is also pretty easy.
We’ll use the same script discussed in the previous lesson to show how to use timeit module inside our code:
Here we check to see if the script is being run directly (i.e. not
imported). If it is, then we import timeit, create a setup string to
import the function into timeit’s namespace and then we call
timeit.timeit. Please note that we pass a call to the function in quotes, then the setup string. And that’s really all there is to it!