This repository benchmarks three languages — Rust, Zig, and C — for generating Ornstein–Uhlenbeck (OU) stochastic processes, commonly used in stochastic calculus and mathematical finance.
We simulate 1,000 trajectories, each of length 500,000, using a simple Euler scheme. Each trajectory uses normally distributed increments to generate a mean-reverting process.
Mathematically, the OU process is given by:
[ dX_t = \theta (\mu - X_t),dt + \sigma,dW_t ]
where:
- (\theta) = mean reversion speed
- (\mu) = long-term mean
- (\sigma) = volatility
- (dW_t) = standard Brownian motion increment
As the maintainer of stochastic-rs, I'm a big fan of low-level, hardware-oriented programming languages, especially for scientific computing.
I’m not an expert in Zig or C, so this benchmark represents what might happen if a "regular Rust user" tries to quickly hack together similar code in other languages after a bit of Googling and reading docs.
- Uses
rand
andrand_distr
- Run with:
cargo run --release
- Uses zprob
- Run with:
zig build run -Doptimize=ReleaseFast
- Simple Box-Muller method for normal sampling
- Compile with:
gcc -O3 -march=native -o main main.c -lm
Természetesen! Íme vesszők nélkül, szépen formázva:
Language | Avg (ms) | Total (ms) |
---|---|---|
Rust 🦀 | 3.08 ms | 3077 ms (~3.1 sec) |
Zig ⚡ | 3.58 ms | 3584 ms (~3.6 sec) |
C 👴 | 3.61 ms | 3611 ms (~3.6 sec) |
This benchmark is not intended to be a definitive performance proof, but rather a fun exploration. Contributions, improvements, or corrections are highly welcome!
MIT
Enjoy, and happy benchmarking! 🚀