Skip to content

Commit 2a2dc85

Browse files
lpozoclaudebzaczynski
authored
Sample code for: Python 3.15 Preview: Upgraded JIT Compiler (#777)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Bartosz Zaczyński <bartosz.zaczynski@gmail.com>
1 parent 046b5de commit 2a2dc85

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

python315-jit-compiler/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python 3.15 Preview: Upgraded JIT Compiler
2+
3+
This folder provides the code examples for the Real Python tutorial [Python 3.15 Preview: Upgraded JIT Compiler](https://realpython.com/python315-jit-compiler/)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Quick benchmark: compare CPython 3.15 with the JIT off vs on.
2+
3+
Run twice against the same 3.15 build and compare the two timings:
4+
5+
PYTHON_JIT=0 python quick_bench.py
6+
PYTHON_JIT=1 python quick_bench.py
7+
"""
8+
9+
import sys
10+
from timeit import timeit
11+
12+
ITERATIONS = 20_000_000
13+
REPEATS = 5
14+
15+
16+
def workload():
17+
x = 1.0
18+
for _ in range(ITERATIONS):
19+
x = x * 1.0001
20+
return x
21+
22+
23+
def jit_enabled():
24+
jit = getattr(sys, "_jit", None)
25+
return bool(jit and jit.is_enabled())
26+
27+
28+
def main():
29+
seconds = timeit(workload, number=REPEATS) / REPEATS
30+
label = "JIT on" if jit_enabled() else "JIT off"
31+
print(f"{label}: {seconds:.2f} s")
32+
33+
34+
if __name__ == "__main__":
35+
main()

0 commit comments

Comments
 (0)