Setting the Baseline

After all the theory on Python concurrency and the Python GIL, we are now ready for some example code and experiments. Let’s get to work!

Our test function

Let’s first define a function that we can use to benchmark our different options. All the following examples use the same function, called heavy:

def heavy(n, myid):
  for x in range(1, n):
    for y in range(1, n):
      x**y
  print(myid, "is done")Code language: Python (python)

The heavy function is a nested Python for-loop that calculates powers (x**y) and throws the results away. It is a CPU-bound function. If you observe your system while running this, you’ll see CPU usage close to 100% (for one core). You can replace it with anything you want, but beware of race conditions — don’t use shared objects or variables.

We’ll be running this function in different ways and explore the differences between a regular, single-thread Python program, multithreading, and multiprocessing.

The baseline: single threaded execution

Each Python program has at least one thread: the main thread. Below you’ll find the single-threaded version, which serves as our baseline in terms of speed. It runs our heavy function 80 times, sequentially:

import time

# A CPU heavy calculation, just
# as an example. This can be
# anything you like
def heavy(n, myid):
    for x in range(1, n):
        for y in range(1, n):
            x**y
    print(myid, "is done")

def sequential(n):
    for i in range(n):    
        heavy(500, i)

if __name__ == "__main__":
    start = time.perf_counter()
    sequential(80)
    end = time.perf_counter()
    print("Took: ", end - start)Code language: Python (python)

On my system, this takes about 46 seconds to run to completion. A newer computer will be faster, but that doesn’t matter: what we’re interested in is how the threaded and multiprocessing versions compare to this baseline.

We measure the time with time.perf_counter(). It’s meant for measuring durations, and unlike time.time(), it isn’t affected when your computer’s clock gets adjusted.

The if __name__ == "__main__": part isn’t needed for this sequential version. You will need it for the multiprocessing version, especially on Windows and macOS, and it’s good form to always use it.

A note on free-threaded Python: the results in this series come from the regular Python build, which has the GIL. Since Python 3.13, there’s also an optional free-threaded build without the GIL (python3.15t, for example). The baseline takes about the same time on both builds, but on the free-threaded build, the threaded version in the next article can use multiple CPU cores and becomes much faster.

In the following articles, we’ll explore a threaded version and a multiprocessing version and learn the difference between these two ways of writing concurrent code.

Related articles

Leave a Comment