From c01e5eaaee0038de8d60effdeee1befa7d52bdd9 Mon Sep 17 00:00:00 2001 From: Leodanis Pozo Ramos Date: Fri, 29 May 2026 12:22:39 +0000 Subject: [PATCH] Sample code for: Python 3.15 Preview: Upgraded JIT Compiler Co-Authored-By: Claude Opus 4.8 (1M context) --- python315-jit-compiler/README.md | 3 +++ python315-jit-compiler/quick_bench.py | 35 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 python315-jit-compiler/README.md create mode 100644 python315-jit-compiler/quick_bench.py diff --git a/python315-jit-compiler/README.md b/python315-jit-compiler/README.md new file mode 100644 index 0000000000..054383533d --- /dev/null +++ b/python315-jit-compiler/README.md @@ -0,0 +1,3 @@ +# Python 3.15 Preview: Upgraded JIT Compiler + +This folder provides the code examples for the Real Python tutorial [Python 3.15 Preview: Upgraded JIT Compiler](https://realpython.com/python315-jit-compiler/) diff --git a/python315-jit-compiler/quick_bench.py b/python315-jit-compiler/quick_bench.py new file mode 100644 index 0000000000..25b480eee8 --- /dev/null +++ b/python315-jit-compiler/quick_bench.py @@ -0,0 +1,35 @@ +"""Quick benchmark: compare CPython 3.15 with the JIT off vs on. + +Run twice against the same 3.15 build and compare the two timings: + + PYTHON_JIT=0 python quick_bench.py + PYTHON_JIT=1 python quick_bench.py +""" + +import sys +from timeit import timeit + +ITERATIONS = 20_000_000 +REPEATS = 5 + + +def workload(): + x = 1.0 + for _ in range(ITERATIONS): + x = x * 1.0001 + return x + + +def jit_enabled(): + jit = getattr(sys, "_jit", None) + return bool(jit and jit.is_enabled()) + + +def main(): + seconds = timeit(workload, number=REPEATS) / REPEATS + label = "JIT on" if jit_enabled() else "JIT off" + print(f"{label}: {seconds:.2f} s") + + +if __name__ == "__main__": + main()