Answer#
There is no single "Rust speed-up". This thread shows one number you can check, with its limits, so that you can judge how far it transfers to your program. The task is deliberately small and CPU-bound: find the starting number below 300,000 with the longest Collatz chain (the answer is 230631 with 442 steps in every language, which is how the programs were checked against each other). It uses only integer arithmetic in a loop, with no allocation and no input or output.
Each program times only its own loop with the language's own clock, so process start-up and the JVM or .NET runtime start are not counted, but just-in-time compilation is. Each was run five times in turn.
| Language | Median of 5 runs (ms) | Fastest run (ms) | Median compared with Rust |
|---|---|---|---|
| Rust (release build) | 53.0 | 51.3 | 1.0 |
| Go | 92.4 | 84.2 | about 1.7 |
| C# (.NET, Release) | 130.7 | 125.0 | about 2.5 |
| Java | 139.0 | 128.4 | about 2.6 |
| JavaScript (Node.js) | 437.8 | 428.6 | about 8 |
| Python (CPython, no extensions) | 7147.4 | 6570.8 | about 135 |
The Rust program:
use std::time::Instant;
fn steps(mut n: u64) -> u32 {
let mut s = 0;
while n != 1 {
n = if n % 2 == 0 { n / 2 } else { 3 * n + 1 };
s += 1;
}
s
}
fn main() {
let limit: u64 = std::env::args().nth(1).map(|a| a.parse().unwrap()).unwrap_or(300_000);
let t = Instant::now();
let mut best = (0u64, 0u32);
for n in 1..limit {
let s = steps(n);
if s > best.1 {
best = (n, s);
}
}
println!("rust n={} steps={} ms={:.1}", best.0, best.1, t.elapsed().as_secs_f64() * 1000.0);
}The Python program, written the same way:
import sys, time
def steps(n):
s = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
s += 1
return s
limit = int(sys.argv[1]) if len(sys.argv) > 1 else 300_000
t = time.perf_counter()
best_n, best_s = 0, 0
for n in range(1, limit):
s = steps(n)
if s > best_s:
best_n, best_s = n, s
print(f"python n={best_n} steps={best_s} ms={(time.perf_counter() - t) * 1000:.1f}")The other four programs (Go, Java, C#, JavaScript) use the same loop with the same types of arithmetic (64-bit unsigned or signed integers; in JavaScript the numbers are doubles).
What this does not show#
- One machine, one task. The runs were made on a laptop with an Intel Core i7-10750H (12 logical processors) while other work was running on it, at below-normal priority. The spread between runs (for example Go from 84 to 219 ms) is real noise. Treat differences below about 20 percent as unproven.
- Integer loops favour compiled languages. Programs that spend their time in a database, a network call, a disk or a library written in C (for example Python code that calls numpy) will not show these gaps. A Python program that is already dominated by such calls gains little from a port.
- No memory measurement. Rust programs usually need less memory than the same program on a garbage-collected runtime, but this thread did not measure it, so no number is claimed.
- Defaults only. No flags were tuned for any language, and the JIT-based runtimes were not warmed up in a separate step.
- Your code is not this code. Allocation patterns, data structures and library choices change the result more than the language label.
How to do it#
- Pick the one function or request that is slow in production, not the whole program.
- Write a small benchmark for that function in the old language and record its result and time on your own machine.
- Port that function and compare the outputs first, then the time (the thread on shadow mode and the one on golden files show how to check outputs).
- Build with
cargo build --release. Timing a debug build says nothing about Rust's speed (see the thread on why a port can be slower than the original).
Related questions#
Will a port help if my program waits for a database most of the time?#
Usually little. The time is spent waiting, and the language cannot make the database faster. Look at the queries and the connection use first.
Can I get most of the gain without rewriting everything?#
Often yes, when one hot function dominates. Write that function in Rust and call it from the old language (see the threads on PyO3, ctypes and napi-rs). The PyO3 thread measures this on the same task.
Sources#
- The Cargo Book, "Profiles": https://doc.rust-lang.org/cargo/reference/profiles.html
std::time::Instant: https://doc.rust-lang.org/std/time/struct.Instant.html