It is very useful to time small snippets of python code.
For example, you might want to do that to debug performance bottlenecks or to compare the efficiencies of different ways to perform a task.
The timeit module is a handy python module that allows you to do just that.
Even though it is pretty straight forward to use this module to time one-line statements, you might find it a little tricky to time multiple-line statements.
In this article, I will teach you how to use this awesome python module to time multiple lines.
But before we dive into how to do that, let’s revisit the different ways you can use the timeit module.
for example, say you want to time this statement
L = [2 ** n for n in range(10)]
There are two ways you can do this
First: From python code
In your python code, you can jump import the module and use the timeit method.
>>> import timeit >>> timeit.timeit('L = [2 ** n for n in range(10)]') 3.0072080040117726
Second: From the command line interface
Another way is to just invoke the timeit module from the shell like this
$ python3 -m timeit 'L = [2 ** n for n in range(10)]' 100000 loops, best of 3: 2.99 usec per loop
Seems pretty straightforward, right?
Now how can you use the timeit module to time a snippet of python code that spans multiple lines?
Let’s discuss how you can do that from python code and from the command line interface.
Timing multiple lines in python code
Timing multiple-line code snippets frome python code is very simple
The easiest way to time multiple lines having the same indentation is to use semicolons to separate the lines.
For example, say you want to time the following snippet.
x = 2 L = [x ** n for n in range(10)]
your code will look like this
>>> import timeit >>> timeit.timeit('x = 2; L = [x ** n for n in range(10)]') 3.0759821450337768
But what if your code have different indentations?
The easiest way to handle different indentations is to to define your code snippet as a string first using triple quotes.
The reason why you should use triple quotes is because it allows you to define strings that can span multiple lines.
For example, you can do something like this
import timeit s = """\ L =  for n in range(10): L.append(2 ** n)""" t = timeit.timeit(s)
Prefect, now let’s discuss how you can do the same thing but from the command line interface.
Timing multiple lines in the command-line interface
For multiple lines that have the same indentation, you can still use semicolons like you did in the previous case.
$ python3 -m timeit 'x = 2; L = [x ** n for n in range(10)]' 100000 loops, best of 3: 3.07 usec per loop
But this is not the only way.
You can also specify each line as a separate statement argument like this
$ python3 -m timeit 'x = 2' 'L = [x ** n for n in range(10)]' 100000 loops, best of 3: 3.03 usec per loop
This separation of statements into different arguments allows us to specify indentations as well.
Let’s see how.
For multiple lines with different indentations, you specify each line as a separate argument and you also preserve the leading spaces.
For example, to time
L =  for n in range(10): L.append(2 ** n)
you can do something like this:
$ python3 -m timeit \ 'L = ' 'for n in range(10):' ' L.append(2 ** n)' 100000 loops, best of 3: 3.47 usec per loop
If you are a beginner, then I highly recommend this book.
No longer a beginner?
Then it’s time to take your Python skills to the next level with this book (It’s my favorite).
Are you Beginning your Programming Career?
I provide my best content for beginners in the newsletter.
- What programming language to start with?
- Do you need a CS degree to be a programmer?
- Career tips and advice
- Programming tutorials
And so much more…