Introduction to Benchmarking
Let's get familiar with the idea of benchmarking.
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:
Create a free account to view this lesson.
Continue your learning journey with a 14-day free trial.
By signing up, you agree to Educative's Terms of Service and Privacy Policy