Skip to content

rust-dd/probability-benchmark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦀 Probability Benchmark: Rust vs Zig vs C

This repository benchmarks three languages — Rust, Zig, and C — for generating Ornstein–Uhlenbeck (OU) stochastic processes, commonly used in stochastic calculus and mathematical finance.

💡 What is it about?

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

⚡ Motivation

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.


💻 Code snippets

Rust (🦀)

  • Uses rand and rand_distr
  • Run with: cargo run --release

See code here


Zig (⚡)

  • Uses zprob
  • Run with: zig build run -Doptimize=ReleaseFast

See code here


C (👴)

  • Simple Box-Muller method for normal sampling
  • Compile with: gcc -O3 -march=native -o main main.c -lm

See code here


Természetesen! Íme vesszők nélkül, szépen formázva:


📊 Results

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)

🗣️ Discussion

This benchmark is not intended to be a definitive performance proof, but rather a fun exploration. Contributions, improvements, or corrections are highly welcome!

👉 Join the discussion


📎 License

MIT


Enjoy, and happy benchmarking! 🚀

Releases

No releases published

Packages

No packages published