Last active
October 26, 2024 13:53
-
-
Save thomasaarholt/2e4d42cbf3f0de60c811bbf7a317c8fd to your computer and use it in GitHub Desktop.
Benchmark for comparing python 3.12 and 3.13 on list comprehensions with a filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from time import time | |
N_repeats = 100 | |
def func(foo: list[int]): | |
return [x for x in foo if x % 2 == 0] | |
print(f"Running on Python {sys.version}") | |
for N in (100, 1_000, 10_000, 100_000, 1_000_000): | |
foo = list(range(N)) | |
times: list[float] = [] | |
for _ in range(N_repeats): | |
t1 = time() | |
func(foo) | |
t2 = time() | |
times.append(t2 - t1) | |
comprehension_time = min(times) | |
time_per_iter_ns = comprehension_time * 1e9 / N | |
print(f"{time_per_iter_ns:.2f}ns on N={N:_}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are mine, expanded to include 3.11 and a compiled free-threading 3.13t build:
I also ran them with Hyperfine: