October 2025
Intermediate to advanced
485 pages
13h 2m
English
One of the easiest ways to benchmark Python code is with Python’s timeit module. In the words of Python’s documentation: “This module provides a simple way to time small bits of Python code.”
Usually, the timeit module is used from the command line to measure small snippets of code, but since we’re going to use it to measure more complex algorithms, we’re going to call the module from within an actual code file.
Let’s start with a basic example to see how timeit works. In Python, you can create an array containing one million integers (0 through 999999) with the following code:
| | array = [] |
| | for i in range(1_000_000): |
| | array.append(i) |
(Note that Python allows us to use underscores to make long numbers more readable.) ...
Read now
Unlock full access