rust-randbench/README.md
2026-01-18 10:41:59 +01:00

5.5 KiB

Benchmark of Rust PRNG crates

Simple performance benchmark of pseudorandom generation Rust crates for non-cryptographic purposes such as games.

The goal is not to compare algorithms, but implementations that can be used immediately by a Rust developer.

Inclusion criteria:

  • Seedable. Needed for reproducibility.
  • Simple to use, either thanks to an obvious API or examples in documentation.
  • Works on common targets.
  • Usable for generating bytes or groups of bytes.
  • Builds on my system.

Warning: If your need has anything to do with security, i.e. generating values seeded by or interacting with potentially untrusted data or that should be secret or not leak information about the seed or correlation between multiple outputs, then you should instead use a CSPRNG.

Results

RUSTFLAGS='-C target-cpu=native' cargo run --release

Results on AMD Ryzen 7 5700X: (with AVX2)

Time (s)        PRNG
0.06681618      frand::Rand
0.06935108      simd_rand::portable::xoshiro256plusplusx4::Xoshiro256PlusPlusX4
0.07013825      simd_rand::specific::avx2::xoshiro256plus::Xoshiro256PlusX4
0.070464075     simd_rand::specific::avx2::xoshiro256plusplus::Xoshiro256PlusPlusX4
0.07331398      simd_rand::portable::xoshiro256plusx8::Xoshiro256PlusX8
0.07332602      simd_rand::portable::xoshiro256plusplusx8::Xoshiro256PlusPlusX8
0.07440938      simd_rand::portable::xoshiro256plusx4::Xoshiro256PlusX4
0.077901766     fast_rands::RomuDuoJrRand
0.07997875      rand_xoshiro::xoshiro256plus::Xoshiro256Plus
0.08022531      wyhash::v1::traits::WyRng
0.08147533      romu::Rng
0.082410775     fast_rands::RomuTrioRand
0.08576214      rand_xoshiro::xoshiro256starstar::Xoshiro256StarStar
0.08702609      wyhash::final3::traits::WyRng
0.087389685     wyrand::final_v4_2::wyrand::WyRand
0.08785791      tinyrand::wyrand::Wyrand
0.08805069      fastrand::Rng
0.088699095     simd_rand::specific::avx2::shishua::Shishua<32768>
0.089351326     rand_xoshiro::xoroshiro128plus::Xoroshiro128Plus
0.090053834     fast_rands::Xoshiro256PlusPlusRand
0.09293575      rand::rngs::small::SmallRng
0.09571917      rand_xoshiro::xoshiro256plusplus::Xoshiro256PlusPlus
0.09630984      rand_xoshiro::xoshiro512plus::Xoshiro512Plus
0.09719508      tiny_prng::pcg::PcgXslRr6432Mcg
0.10209641      rand_xoshiro::xoshiro512starstar::Xoshiro512StarStar
0.10582184      rand_xoshiro::xoroshiro128starstar::Xoroshiro128StarStar
0.107617944     rand_xoshiro::xoshiro512plusplus::Xoshiro512PlusPlus
0.11635949      tinyrand::xorshift::Xorshift
0.11716504      rand_xoshiro::xoroshiro128plusplus::Xoroshiro128PlusPlus
0.12221907      shishua::core::ShiShuAState
0.1273574       tiny_prng::pcg::PcgXslRrMcg
0.13119678      tiny_prng::xorshift::Xorshift1024star
0.13813396      tiny_prng::xorshift::Xorshift128
0.14830554      rand_xoshiro::splitmix64::SplitMix64
0.1490197       fast_rands::Sfc64Rand
0.15220404      tiny_prng::pcg::PcgXslRr
0.15493397      tinyrand::splitmix::SplitMix
0.16185075      rand_xoshiro::xoshiro128plus::Xoshiro128Plus
0.17024948      bsd4random::BSD
0.17189687      prng_mt::mt19937::MT19937_64
0.1737906       fast_rands::XorShift64Rand
0.17380002      tiny_prng::xorshift::Xorshift64
0.17656836      fast_rands::Lehmer64Rand
0.18433817      mcg59::MCG59
0.18995807      tiny_prng::xorshift::Xorshift64star
0.19150706      rand_xoshiro::xoshiro128plusplus::Xoshiro128PlusPlus
0.19262685      rand_xoshiro::xoshiro128starstar::Xoshiro128StarStar
0.20068832      tiny_prng::mt64::Mt19937
0.2185049       aes_prng::AesRng
0.23102058      rand_xoshiro::xoroshiro64star::Xoroshiro64Star
0.23144223      tiny_prng::pcg::PcgXshRs6432
0.23157886      tiny_prng::pcg::PcgXshRr6432
0.25709888      rand_xoshiro::xoroshiro64starstar::Xoroshiro64StarStar
0.28615955      rand::rngs::std::StdRng
0.29523018      prng_mt::mt19937::MT19937
0.31545836      quad_rand::RandGenerator
0.34703827      tiny_prng::xorshift::Xorshift32
0.34762907      nanorand::rand::wyrand::WyRand
0.39694864      tiny_prng::mt::Mt19937
0.46873802      nanorand::rand::pcg64::Pcg64
0.8413978       nanorand::rand::chacha::ChaCha<8>
1.769573        shishua::rand::ShiShuARng
3.5496118       acorn_prng::Acorn
3.7084286       simplerand::Random

FRand is the fastest, however it has no particular mathematical justification beyond the fact its parameters were found empirically to minimize bias.

simd_rand provides the fastest implementations of common PRNGs. The best on my computer (without AVX512) was a portable one, probably thanks to compiler autovectorization. Sadly this crate is not published on crates.io, only on GitHub.

fast_rands implements RomuDuoJr, the fastest after simd_rand. It is part of the Romu family which has been studied. It provides good quality up to at least 2^48 bytes. However this crate has too many dependencies that should not be necessary if you only want a PRNG, so copying the wanted functions can be an option.

Surprisingly, the AES PRNG is quite fast, probably due to the AES instruction set.

Also to my surprise, Shishua was slower than RomuTrio, contrarily to the author's claim. Maybe that's the implementation's fault or my lack of AVX512, I should investigate. The streaming overhead can be significant, as we see with ShiShuARng which is very slow compared to the bare ShiShuAState. simd_rand's implementation is quite faster.