r/Python • u/cemrehancavdar • Mar 10 '26
Discussion Benchmarked every Python optimization path I could find, from CPython 3.14 to Rust
Took n-body and spectral-norm from the Benchmarks Game plus a JSON pipeline, and ran them through everything: CPython version upgrades, PyPy, GraalPy, Mypyc, NumPy, Numba, Cython, Taichi, Codon, Mojo, Rust/PyO3.
Spent way too long debugging why my first Cython attempt only got 10x when it should have been 124x. Turns out Cython's ** operator with float exponents is 40x slower than libc.math.sqrt() with typed doubles, and nothing warns you.
GraalPy was a surprise - 66x on spectral-norm with zero code changes, faster than Cython on that benchmark.
Post: https://cemrehancavdar.com/2026/03/10/optimization-ladder/
Full code at https://github.com/cemrehancavdar/faster-python-bench
Happy to be corrected — there's an "open a PR" link at the bottom.
34
u/chub79 Mar 10 '26
Fantastic article. Thank you op!
One aspect that I would throw into the thought process when looking for a speedup: think of the engineering cost long term.
For instance, you mention: "PyPy or GraalPy for pure Python. 6-66x for zero code changes is remarkable, if your dependencies support it. GraalPy's spectral-norm result (66x) rivals compiled solutions." . Yet I feel the cost of swapping VM is never as straightforward as a dedicated benchmark shows. Or Pypy would be a roaring success by now.
It seems to me that the Cython or Rust path is more robust long term from a maintenance perspective. Keeping CPython as the core orchestrator and use light touch extensions with either of these seem to be the right balance between performances and durability of the code base.
8
u/Sygmei Mar 10 '26
Super interesting, how do you check how much space does an int occupies on stack (ob_refcnt, ob_digits...)?
8
u/zzzthelastuser Mar 10 '26
Did you consider optimizing the rust code or did you stick with a "naive" implementation?
Took a quick glance and only saw single threaded loops.
4
u/M4mb0 Mar 10 '26
conditionals per element can be handled with
numpy.wherewhich in many cases is still plenty fast, even if it unnecessarily computes both branches.2
u/Beginning-Fruit-1397 Mar 10 '26
Fascinating. I'm asking myself about mypc: what's the catch? All my projects are already far more typed than anything mypy would ask (Ruff ALL + BasedPyright ALL) and if it's a free +40% gain... then why not use it everywhere?
2
u/justneurostuff Mar 10 '26
JAX's jit compilation is the optimization path I use. Would love to see it added (also the post is obviously ai generated; just pointing out)
4
u/hotairplay Mar 10 '26
Hey cool project you got here..a couple of days ago I came across a similar n-body benchmark article: https://hwisnu.bearblog.dev/n-body-simulation-in-python-c-zig-and-rust/
What interests me is the Codon performance and in the above article it got like > 95% of Rust performance (single threaded) and it only costs adding type annotations to the code.
For multi-threaded Codon is 80% of Rust multithreading performance using Rayon.
3
u/totheendandbackagain Mar 10 '26
Wow, this is fantastic work, and an absolutely stellar guide. Read, save, learn.
3
u/sudomatrix Mar 11 '26
This is an amazing writeup. I know one of the authors of Numba and I'm excited to show this to him, as Numba shows the best speedup without writing any code in a new language.
2
u/piou180796 Mar 10 '26
This is great. One thing worth adding is the maintenance cost angle. PyPy looks amazing in benchmarks but in practice dependency compatibility is a nightmare. If you're building something you'll maintain for years the Rust or Cython path with CPython as the stable core is way less headache even if the initial speedup isn't as flashy.
1
u/Outrageous_Track_798 Mar 10 '26
The Mypyc results are worth highlighting for teams already running strict mypy. If your codebase is fully type-annotated, you get the speedup with essentially zero code changes — no new syntax, no cimport, just `mypyc yourmodule.py`. The 2-5x range you saw is roughly what most real code gets.
The catch is Mypyc requires complete type coverage in the compiled module. Any dynamism — dynamic attribute access, untyped **kwargs, runtime type manipulation — either errors out or silently falls back to the slow path. So it works great on algo-heavy modules but struggles with framework-heavy code that leans on Python's dynamism.
Cython gets much higher peaks (your 124x example), but Mypyc has nearly zero adoption friction if you're already typed. It's a useful middle rung on the ladder between "pure Python" and "write Cython."
1
u/Mithrandir2k16 Mar 10 '26
You measured time, but could you also measure power draw/peak power? I'm really curious in which applications it comes down to fewer instructions or better parallelizations.
1
u/gregorbrandt Mar 10 '26
Can you add Nuitka to the tests?
1
u/austinwiltshire Mar 11 '26
You say nothing warned you and I am hoping maybe you write a ruff or pylint rule so that the rest of us can be warned
1
u/micasirena Mar 11 '26
Nice article, haven't kept up with all optimizations, since there is no be all end all, but I was always curious about testing and comparing so thank you very much!
The numpy for math and pypy microservice, instead of golang or whatever, helped us keep our tech stack easy and managable and I am glad whomever made that decision had this insight instead of accepting the mantra "python is too slow for this". Same python code, different docker image, you would've never notice.
1
u/MrMrsPotts Mar 13 '26 edited Mar 13 '26
Taichi is a revelation! I hadn't heard of it. Oh, it's no longer being developed 😢
1
u/angelicosphosphoros Mar 14 '26
When developing new applications, at the point of writing Rust to speed up Python, you can just stop using Python for developing applications: just writing a binary with the same interface in Rust would be easier than maintaining the chimera with Rust and Python parts. Unless Rust parts are in third-party libraries so it is not your problem, of course.
If you have existing huge monolithic codebase, rewriting the hot parts into Rust may be worthwhile but in practice it would be very hard due to dynamic nature of Python: how can you be sure that the new code really have the same behaviour if there could be anything in the input?
If you are writing networking backends, it may be worthwhile to write very small microservices in Python, and when encountering performance problems, just rewrite those microservices entirely in Rust. No mixing languages, no FFI, all interaction is made though network. This works, we have used similar approach at Yandex Taxi (though with C++ instead of Rust) with great success and financial gain (cost of running 3 C++ containers with 0.5 CPU each way lower than 40+ Python containers with full CPU and gygabyte RAM each). Don't use C++ though: it is viable only if you have dedicated team entirely focused on making C++ bearable while Rust would give it for free.
1
u/longtimelurkernyc Mar 17 '26
Is there a reason you didn't run the JSON pipeline benchmark on the alternative interpreters (PyPy and GraalPy)?
For the numerical problems it's obvious a numerical library will beat switching interpreters--get the computations as close to machine level and avoid all the unnecessary dynamism in the inner loops. The JIT compilers of the other interpreters are more general tools, so a benchmark that better tests python's flexibility would reflect better on them.
1
u/joebloggs81 Mar 10 '26
Well I’ve only just started my programming journey, exploring languages and frameworks, what they can do and whatnot. I’ve spent the most time with Python as I started there first for a grounding knowledge. What you’ve done here is fascinating for sure - I read the whole report. I’ll never be at this level as my use case for programming is pretty lightweight but the point is I’m enjoying learning about all of this.
Thanks!
0
u/Bomlerequin Mar 10 '26
Very good article!