CPU-bound matrix operations benchmark comparing sequential, multithreaded, and multiprocessing execution in Python.
cpu_bound is a benchmark designed to explore CPU-bound performance in Python. It compares single-threaded, multi-threaded, and multi-process execution across matrix addition, multiplication, and transposition to analyze Python's Global Interpreter Lock (GIL), process serialization overhead, and multi-core scaling.
- Python 3.10+
- Standard library only (
argparse,concurrent.futures,time,random,typing,abc,logging)
python3 cpu_bench.py [options]| Flag | Long Flag | Description | Default |
|---|---|---|---|
-r |
--rows |
Number of rows (required) | — |
-c |
--cols |
Number of columns (required) | — |
-o |
--op |
Operation: add, mult, transpose |
add |
-m |
--mode |
Execution mode: sequential, threaded, parallel |
sequential |
--random |
Fill matrices with random floats instead of zeros | False |
|
-h |
--help |
Show usage information | — |
| Mode | Implementation | CPU Cores Used | Behavior on CPU-Bound Tasks |
|---|---|---|---|
sequential |
Single thread | 1 Core | Baseline single-core execution. |
threaded |
ThreadPoolExecutor |
1 Core (at a time) | Constrained by the GIL. Bytecode is serialized; switching overhead makes it equal to or slower than sequential. |
parallel |
ProcessPoolExecutor |
All Available Cores | Bypasses the GIL by spawning worker processes with independent interpreters. Delivers true multi-core speedup for compute-heavy tasks. |
# Sequential multiplication (baseline)
python3 cpu_bench.py -r 500 -c 500 --random -m sequential -o mult
# Multithreaded multiplication (GIL-bound)
python3 cpu_bench.py -r 500 -c 500 --random -m threaded -o mult
# Multiprocessing multiplication (True multi-core parallelism)
python3 cpu_bench.py -r 500 -c 500 --random -m parallel -o mult# Sequential addition
python3 cpu_bench.py -r 2000 -c 2000 --random -m sequential -o add
# Multithreaded addition
python3 cpu_bench.py -r 2000 -c 2000 --random -m threaded -o add
# Multiprocessing addition
python3 cpu_bench.py -r 2000 -c 2000 --random -m parallel -o addNote
-
Matrix Multiplication (
mult): Heavy computation heavily outweighs inter-process communication (IPC) costs, so-m parallelyields significant speedups. -
Matrix Addition (
add): The computation is lightweight ($O(N^2)$), meaning the overhead of serializing (pickling) large matrices between worker processes can makeparallelslower thansequential. -
Threads and the GIL: In standard CPython,
-m threadedcannot run pure Python bytecode simultaneously on multiple cores due to the Global Interpreter Lock.
This project is licensed under the MIT License. See LICENSE for details.