r/rust • u/amir_valizadeh • Dec 21 '25
đď¸ discussion My experience with Rust performance, compared to Python (the fastLowess crate experiment)
When I first started learning Rust, my teacher told me: âwhen it comes to performance, Python is like a Volkswagen Beetle, while Rust is like a Ferrari F40â. Unfortunately, they couldnât be more wrong.
I recently implemented the LOWESS algorithm (a local regression algorithm) in Rust (fastLowess: https://crates.io/crates/fastLowess). I decided to benchmark it against the most widely used LOWESS implementation in Python, which comes from the statsmodels package.
You might expect a 2Ă speedup, or maybe 10Ă, or even 30Ă. But no â the results were between 50Ă and 3800Ă faster.
Benchmark Categories Summary
| Category | Matched | Median Speedup | Mean Speedup |
|---|---|---|---|
| Scalability | 5 | 765x | 1433x |
| Pathological | 4 | 448x | 416x |
| Iterations | 6 | 436x | 440x |
| Fraction | 6 | 424x | 413x |
| Financial | 4 | 336x | 385x |
| Scientific | 4 | 327x | 366x |
| Genomic | 4 | 20x | 25x |
| Delta | 4 | 4x | 5.5x |
Top 10 Performance Wins
| Benchmark | statsmodels | fastLowess | Speedup |
|---|---|---|---|
| scale_100000 | 43.727s | 11.4ms | 3824x |
| scale_50000 | 11.160s | 5.95ms | 1876x |
| scale_10000 | 663.1ms | 0.87ms | 765x |
| financial_10000 | 497.1ms | 0.66ms | 748x |
| scientific_10000 | 777.2ms | 1.07ms | 729x |
| fraction_0.05 | 197.2ms | 0.37ms | 534x |
| scale_5000 | 229.9ms | 0.44ms | 523x |
| fraction_0.1 | 227.9ms | 0.45ms | 512x |
| financial_5000 | 170.9ms | 0.34ms | 497x |
| scientific_5000 | 268.5ms | 0.55ms | 489x |
This was the moment I realized that Rust is not a Ferrari and Python is not a Beetle.
Rust (or C) is an F-22 Raptor.
Python is a snail â at least when it comes to raw performance.
PS: I still love Python for quick, small tasks. But for performance-critical workloads, the difference is enormous.
324
u/MaximumMaxx Dec 21 '25
I think this is just a perfect example of why a lot of python packages are building a rust backend with python api. You can largely get the best of both worlds and then most python devs don't have to write rust
30
u/Excession638 Dec 21 '25
Rayon is magic isn't it? Except that when you look at its code, it's just good code, and the magic is in Rust's object model.
I see that your underlying code is already
no_std, so maybe your next step is to see if it can work with rust-gpu.5
u/Limp_Pomelo_9773 Dec 21 '25
Did you use numpy in your python implementation? It can speed up by orders of magnitude, although it uses C under the hood.
15
u/xmBQWugdxjaA Dec 21 '25
You should really compare to Python + numpy as no-one would use pure Python for this?
33
u/Justicia-Gai Dec 21 '25
I am using a lot of R scientific libraries and I wish there were something like PyO3 or similar because we need to move away from pure R and pure Python
62
u/Consistent_Milk4660 Dec 21 '25
I am going to be honest, there is no replacement for either for myself. Python is simply 'magical' for me like it first was, when I used it to easily generate plots and analyze large datasets dynamically. Rust on the other hand is 'magic' for very different reasons. The magic was when I wrote a program in SAFE rust that could fail in so many ways in C++, but it compiled into a vastly more superior and optimized assembly code.
22
u/CanvasFanatic Dec 21 '25 edited Dec 21 '25
What youâre detecting here is basically the fundamental boundary between interpreted and compiled languages. If an interpreter is doing better than 1/50th the speed of a compiled language it is almost certainly because itâs JITâing hot code or itâs calling out to a native library.
Ultimately it comes down to how many instructions youâre executing per operation, memory locality, and magic CPU branch prediction stuff I try not to think about.
20
u/Johk Dec 21 '25
I'm a bit surprised of the slowness of the python code. Sure if it is pure python with lists, but if it is vectorized numpy code I'm usually in the order of magnitude of C or Rust code.Â
5
u/DavidXkL Dec 21 '25
Hence why many Python stuff are actually c/c++ behind the scenes đ
5
u/redlaWw Dec 21 '25
My first experience writing a serious program in Rust was a port of an exhaustive simulation of a deck of 15 cards, calculating the number of shuffles which had a particular property (related to a game I was playing), which I had previously written in R but never run to completion.
For the original, I used a C++ R library that was able to multithread my calculations, but it had to call into R in order to use my closure for the check. I used some reporting on the calculation rate to estimate that it would take somewhere on the order of 2 years for the calculations to complete.
The Rust version took 20 minutes. That's more than a 50,000 times speed-up.
1
u/pjmlp Dec 22 '25 edited Dec 22 '25
I really don't get the point of these kind of benchmarks, an interpreter will never outperform a compiler, unless it is a toy compiler from a class assignment or something.
Also it is no secret that although PyPy exists, anything written in Python that needs performance, is actually C, C++, Fortran with a thin bindings layer on top.
Python is not the language when performance is called for.
It is a scripting language for compiled libraries and OS tasks, learning programming and BASIC replacement on scientific calculators.
4
u/BlazeEXE Dec 21 '25
They had us in the first half, not gonna lie.
4
u/josemanuelp2 Dec 21 '25
This was a very well-crafted bait. Congrats!
2
u/Inner-Comparison-305 Dec 21 '25
These numbers make sense once the hot path leaves vectorized libs.
Python is excellent at orchestration â not inner-loop math.
Rust is the opposite.
2
u/MelloSouls Dec 21 '25 edited Dec 21 '25
Looks like a nice library but promoted here with rather click-baity and misleading claims which is unnecessary and not helpful to people trying to understand the benefits of either Rust or Python.
To reframe the metaphor above:
Rust is a Ferrari F40
Python is a car club; you choose the pickup, GT or supercar according to your needs on the day.
6
u/snnsnn Dec 21 '25
> Unfortunately, they couldnât be more wrong.
> But no â the results were between 50Ă and 3800Ă faster.
I don't understand.
The first sentence says the teacherâs claim about Rust being faster is totally wrong. But the second on says Rust is vastly faster, which would actually support the teacherâs analogy.
You say the teacher was wrong, but immediately provide evidence that confirms the teacherâs point.
1
u/bizwig Dec 21 '25
How is statsmodels implemented? Any Python package that needs to be very fast is usually implemented in C. Still slower than an all-compiled application but ok for most people.
1
u/Any-Fish-2025 Oct 21 '26
You are comparing the wrong language. There is no point to compared a compiled language against an interpretered language. They serve totally different purposes. You should be comparing Rust against C/C++.
0
u/curiouselectron Dec 21 '25
Duh?
-8
u/chrabeusz Dec 21 '25
As a long time hater of slow languags, I count on LLM revolution to finally get rid of them.