diff options
Diffstat (limited to 'rand/benches/generators.rs')
-rw-r--r-- | rand/benches/generators.rs | 128 |
1 files changed, 54 insertions, 74 deletions
diff --git a/rand/benches/generators.rs b/rand/benches/generators.rs index a6e3a42..808bb67 100644 --- a/rand/benches/generators.rs +++ b/rand/benches/generators.rs @@ -7,15 +7,9 @@ // except according to those terms. #![feature(test)] +#![allow(non_snake_case)] extern crate test; -extern crate rand; -extern crate rand_isaac; -extern crate rand_chacha; -extern crate rand_hc; -extern crate rand_pcg; -extern crate rand_xorshift; -extern crate rand_xoshiro; const RAND_BENCH_N: u64 = 1000; const BYTES_LEN: usize = 1024; @@ -25,11 +19,11 @@ use test::{black_box, Bencher}; use rand::prelude::*; use rand::rngs::adapter::ReseedingRng; -use rand::rngs::{OsRng, JitterRng, EntropyRng}; +use rand::rngs::{OsRng, mock::StepRng}; use rand_isaac::{IsaacRng, Isaac64Rng}; -use rand_chacha::ChaChaRng; -use rand_hc::{Hc128Rng, Hc128Core}; -use rand_pcg::{Lcg64Xsh32, Mcg128Xsl64}; +use rand_chacha::{ChaCha20Core, ChaCha8Rng, ChaCha12Rng, ChaCha20Rng}; +use rand_hc::{Hc128Rng}; +use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg}; use rand_xorshift::XorShiftRng; use rand_xoshiro::{Xoshiro256StarStar, Xoshiro256Plus, Xoshiro128StarStar, Xoshiro128Plus, Xoroshiro128StarStar, Xoroshiro128Plus, SplitMix64, @@ -52,6 +46,7 @@ macro_rules! gen_bytes { } } +gen_bytes!(gen_bytes_step, StepRng::new(0, 1)); gen_bytes!(gen_bytes_xorshift, XorShiftRng::from_entropy()); gen_bytes!(gen_bytes_xoshiro256starstar, Xoshiro256StarStar::from_entropy()); gen_bytes!(gen_bytes_xoshiro256plus, Xoshiro256Plus::from_entropy()); @@ -62,15 +57,19 @@ gen_bytes!(gen_bytes_xoroshiro128plus, Xoroshiro128Plus::from_entropy()); gen_bytes!(gen_bytes_xoroshiro64starstar, Xoroshiro64StarStar::from_entropy()); gen_bytes!(gen_bytes_xoroshiro64star, Xoroshiro64Star::from_entropy()); gen_bytes!(gen_bytes_splitmix64, SplitMix64::from_entropy()); -gen_bytes!(gen_bytes_lcg64_xsh32, Lcg64Xsh32::from_entropy()); -gen_bytes!(gen_bytes_mcg128_xsh64, Mcg128Xsl64::from_entropy()); -gen_bytes!(gen_bytes_chacha20, ChaChaRng::from_entropy()); +gen_bytes!(gen_bytes_pcg32, Pcg32::from_entropy()); +gen_bytes!(gen_bytes_pcg64, Pcg64::from_entropy()); +gen_bytes!(gen_bytes_pcg64mcg, Pcg64Mcg::from_entropy()); +gen_bytes!(gen_bytes_chacha8, ChaCha8Rng::from_entropy()); +gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_entropy()); +gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_entropy()); gen_bytes!(gen_bytes_hc128, Hc128Rng::from_entropy()); gen_bytes!(gen_bytes_isaac, IsaacRng::from_entropy()); gen_bytes!(gen_bytes_isaac64, Isaac64Rng::from_entropy()); gen_bytes!(gen_bytes_std, StdRng::from_entropy()); +#[cfg(feature="small_rng")] gen_bytes!(gen_bytes_small, SmallRng::from_entropy()); -gen_bytes!(gen_bytes_os, OsRng::new().unwrap()); +gen_bytes!(gen_bytes_os, OsRng); macro_rules! gen_uint { ($fnn:ident, $ty:ty, $gen:expr) => { @@ -89,6 +88,7 @@ macro_rules! gen_uint { } } +gen_uint!(gen_u32_step, u32, StepRng::new(0, 1)); gen_uint!(gen_u32_xorshift, u32, XorShiftRng::from_entropy()); gen_uint!(gen_u32_xoshiro256starstar, u32, Xoshiro256StarStar::from_entropy()); gen_uint!(gen_u32_xoshiro256plus, u32, Xoshiro256Plus::from_entropy()); @@ -99,16 +99,21 @@ gen_uint!(gen_u32_xoroshiro128plus, u32, Xoroshiro128Plus::from_entropy()); gen_uint!(gen_u32_xoroshiro64starstar, u32, Xoroshiro64StarStar::from_entropy()); gen_uint!(gen_u32_xoroshiro64star, u32, Xoroshiro64Star::from_entropy()); gen_uint!(gen_u32_splitmix64, u32, SplitMix64::from_entropy()); -gen_uint!(gen_u32_lcg64_xsh32, u32, Lcg64Xsh32::from_entropy()); -gen_uint!(gen_u32_mcg128_xsh64, u32, Mcg128Xsl64::from_entropy()); -gen_uint!(gen_u32_chacha20, u32, ChaChaRng::from_entropy()); +gen_uint!(gen_u32_pcg32, u32, Pcg32::from_entropy()); +gen_uint!(gen_u32_pcg64, u32, Pcg64::from_entropy()); +gen_uint!(gen_u32_pcg64mcg, u32, Pcg64Mcg::from_entropy()); +gen_uint!(gen_u32_chacha8, u32, ChaCha8Rng::from_entropy()); +gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_entropy()); +gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_entropy()); gen_uint!(gen_u32_hc128, u32, Hc128Rng::from_entropy()); gen_uint!(gen_u32_isaac, u32, IsaacRng::from_entropy()); gen_uint!(gen_u32_isaac64, u32, Isaac64Rng::from_entropy()); gen_uint!(gen_u32_std, u32, StdRng::from_entropy()); +#[cfg(feature="small_rng")] gen_uint!(gen_u32_small, u32, SmallRng::from_entropy()); -gen_uint!(gen_u32_os, u32, OsRng::new().unwrap()); +gen_uint!(gen_u32_os, u32, OsRng); +gen_uint!(gen_u64_step, u64, StepRng::new(0, 1)); gen_uint!(gen_u64_xorshift, u64, XorShiftRng::from_entropy()); gen_uint!(gen_u64_xoshiro256starstar, u64, Xoshiro256StarStar::from_entropy()); gen_uint!(gen_u64_xoshiro256plus, u64, Xoshiro256Plus::from_entropy()); @@ -119,26 +124,19 @@ gen_uint!(gen_u64_xoroshiro128plus, u64, Xoroshiro128Plus::from_entropy()); gen_uint!(gen_u64_xoroshiro64starstar, u64, Xoroshiro64StarStar::from_entropy()); gen_uint!(gen_u64_xoroshiro64star, u64, Xoroshiro64Star::from_entropy()); gen_uint!(gen_u64_splitmix64, u64, SplitMix64::from_entropy()); -gen_uint!(gen_u64_lcg64_xsh32, u64, Lcg64Xsh32::from_entropy()); -gen_uint!(gen_u64_mcg128_xsh64, u64, Mcg128Xsl64::from_entropy()); -gen_uint!(gen_u64_chacha20, u64, ChaChaRng::from_entropy()); +gen_uint!(gen_u64_pcg32, u64, Pcg32::from_entropy()); +gen_uint!(gen_u64_pcg64, u64, Pcg64::from_entropy()); +gen_uint!(gen_u64_pcg64mcg, u64, Pcg64Mcg::from_entropy()); +gen_uint!(gen_u64_chacha8, u64, ChaCha8Rng::from_entropy()); +gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_entropy()); +gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_entropy()); gen_uint!(gen_u64_hc128, u64, Hc128Rng::from_entropy()); gen_uint!(gen_u64_isaac, u64, IsaacRng::from_entropy()); gen_uint!(gen_u64_isaac64, u64, Isaac64Rng::from_entropy()); gen_uint!(gen_u64_std, u64, StdRng::from_entropy()); +#[cfg(feature="small_rng")] gen_uint!(gen_u64_small, u64, SmallRng::from_entropy()); -gen_uint!(gen_u64_os, u64, OsRng::new().unwrap()); - -// Do not test JitterRng like the others by running it RAND_BENCH_N times per, -// measurement, because it is way too slow. Only run it once. -#[bench] -fn gen_u64_jitter(b: &mut Bencher) { - let mut rng = JitterRng::new().unwrap(); - b.iter(|| { - rng.gen::<u64>() - }); - b.bytes = size_of::<u64>() as u64; -} +gen_uint!(gen_u64_os, u64, OsRng); macro_rules! init_gen { ($fnn:ident, $gen:ident) => { @@ -163,60 +161,42 @@ init_gen!(init_xoroshiro128plus, Xoroshiro128Plus); init_gen!(init_xoroshiro64starstar, Xoroshiro64StarStar); init_gen!(init_xoroshiro64star, Xoroshiro64Star); init_gen!(init_splitmix64, SplitMix64); -init_gen!(init_lcg64_xsh32, Lcg64Xsh32); -init_gen!(init_mcg128_xsh64, Mcg128Xsl64); +init_gen!(init_pcg32, Pcg32); +init_gen!(init_pcg64, Pcg64); +init_gen!(init_pcg64mcg, Pcg64Mcg); init_gen!(init_hc128, Hc128Rng); init_gen!(init_isaac, IsaacRng); init_gen!(init_isaac64, Isaac64Rng); -init_gen!(init_chacha, ChaChaRng); - -#[bench] -fn init_jitter(b: &mut Bencher) { - b.iter(|| { - JitterRng::new().unwrap() - }); -} - +init_gen!(init_chacha, ChaCha20Rng); -const RESEEDING_THRESHOLD: u64 = 1024*1024*1024; // something high enough to get - // deterministic measurements - -#[bench] -fn reseeding_hc128_bytes(b: &mut Bencher) { - let mut rng = ReseedingRng::new(Hc128Core::from_entropy(), - RESEEDING_THRESHOLD, - EntropyRng::new()); - let mut buf = [0u8; BYTES_LEN]; - b.iter(|| { - for _ in 0..RAND_BENCH_N { - rng.fill_bytes(&mut buf); - black_box(buf); - } - }); - b.bytes = BYTES_LEN as u64 * RAND_BENCH_N; -} +const RESEEDING_BYTES_LEN: usize = 1024 * 1024; +const RESEEDING_BENCH_N: u64 = 16; -macro_rules! reseeding_uint { - ($fnn:ident, $ty:ty) => { +macro_rules! reseeding_bytes { + ($fnn:ident, $thresh:expr) => { #[bench] fn $fnn(b: &mut Bencher) { - let mut rng = ReseedingRng::new(Hc128Core::from_entropy(), - RESEEDING_THRESHOLD, - EntropyRng::new()); + let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(), + $thresh * 1024, + OsRng); + let mut buf = [0u8; RESEEDING_BYTES_LEN]; b.iter(|| { - let mut accum: $ty = 0; - for _ in 0..RAND_BENCH_N { - accum = accum.wrapping_add(rng.gen::<$ty>()); + for _ in 0..RESEEDING_BENCH_N { + rng.fill_bytes(&mut buf); + black_box(&buf); } - accum }); - b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N; + b.bytes = RESEEDING_BYTES_LEN as u64 * RESEEDING_BENCH_N; } } } -reseeding_uint!(reseeding_hc128_u32, u32); -reseeding_uint!(reseeding_hc128_u64, u64); +reseeding_bytes!(reseeding_chacha20_4k, 4); +reseeding_bytes!(reseeding_chacha20_16k, 16); +reseeding_bytes!(reseeding_chacha20_32k, 32); +reseeding_bytes!(reseeding_chacha20_64k, 64); +reseeding_bytes!(reseeding_chacha20_256k, 256); +reseeding_bytes!(reseeding_chacha20_1M, 1024); macro_rules! threadrng_uint { |