aboutsummaryrefslogtreecommitdiff
path: root/rand/benches
diff options
context:
space:
mode:
Diffstat (limited to 'rand/benches')
-rw-r--r--rand/benches/distributions.rs259
-rw-r--r--rand/benches/generators.rs128
-rw-r--r--rand/benches/misc.rs60
-rw-r--r--rand/benches/seq.rs9
-rw-r--r--rand/benches/weighted.rs36
5 files changed, 116 insertions, 376 deletions
diff --git a/rand/benches/distributions.rs b/rand/benches/distributions.rs
deleted file mode 100644
index 069a828..0000000
--- a/rand/benches/distributions.rs
+++ /dev/null
@@ -1,259 +0,0 @@
-// Copyright 2018 Developers of the Rand project.
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#![feature(test)]
-
-extern crate test;
-extern crate rand;
-
-const RAND_BENCH_N: u64 = 1000;
-
-use std::mem::size_of;
-use test::Bencher;
-use std::time::Duration;
-
-use rand::{Rng, FromEntropy};
-use rand::rngs::SmallRng;
-use rand::distributions::*;
-
-macro_rules! distr_int {
- ($fnn:ident, $ty:ty, $distr:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
- let distr = $distr;
-
- b.iter(|| {
- let mut accum = 0 as $ty;
- for _ in 0..::RAND_BENCH_N {
- let x: $ty = distr.sample(&mut rng);
- accum = accum.wrapping_add(x);
- }
- accum
- });
- b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-macro_rules! distr_float {
- ($fnn:ident, $ty:ty, $distr:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
- let distr = $distr;
-
- b.iter(|| {
- let mut accum = 0.0;
- for _ in 0..::RAND_BENCH_N {
- let x: $ty = distr.sample(&mut rng);
- accum += x;
- }
- accum
- });
- b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-macro_rules! distr_duration {
- ($fnn:ident, $distr:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
- let distr = $distr;
-
- b.iter(|| {
- let mut accum = Duration::new(0, 0);
- for _ in 0..::RAND_BENCH_N {
- let x: Duration = distr.sample(&mut rng);
- accum = accum.checked_add(x).unwrap_or(Duration::new(u64::max_value(), 999_999_999));
- }
- accum
- });
- b.bytes = size_of::<Duration>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-macro_rules! distr {
- ($fnn:ident, $ty:ty, $distr:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
- let distr = $distr;
-
- b.iter(|| {
- let mut accum = 0u32;
- for _ in 0..::RAND_BENCH_N {
- let x: $ty = distr.sample(&mut rng);
- accum = accum.wrapping_add(x as u32);
- }
- accum
- });
- b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-macro_rules! distr_arr {
- ($fnn:ident, $ty:ty, $distr:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
- let distr = $distr;
-
- b.iter(|| {
- let mut accum = 0u32;
- for _ in 0..::RAND_BENCH_N {
- let x: $ty = distr.sample(&mut rng);
- accum = accum.wrapping_add(x[0] as u32);
- }
- accum
- });
- b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-// uniform
-distr_int!(distr_uniform_i8, i8, Uniform::new(20i8, 100));
-distr_int!(distr_uniform_i16, i16, Uniform::new(-500i16, 2000));
-distr_int!(distr_uniform_i32, i32, Uniform::new(-200_000_000i32, 800_000_000));
-distr_int!(distr_uniform_i64, i64, Uniform::new(3i64, 123_456_789_123));
-distr_int!(distr_uniform_i128, i128, Uniform::new(-123_456_789_123i128, 123_456_789_123_456_789));
-
-distr_float!(distr_uniform_f32, f32, Uniform::new(2.26f32, 2.319));
-distr_float!(distr_uniform_f64, f64, Uniform::new(2.26f64, 2.319));
-
-const LARGE_SEC: u64 = u64::max_value() / 1000;
-
-distr_duration!(distr_uniform_duration_largest,
- Uniform::new_inclusive(Duration::new(0, 0), Duration::new(u64::max_value(), 999_999_999))
-);
-distr_duration!(distr_uniform_duration_large,
- Uniform::new(Duration::new(0, 0), Duration::new(LARGE_SEC, 1_000_000_000 / 2))
-);
-distr_duration!(distr_uniform_duration_one,
- Uniform::new(Duration::new(0, 0), Duration::new(1, 0))
-);
-distr_duration!(distr_uniform_duration_variety,
- Uniform::new(Duration::new(10000, 423423), Duration::new(200000, 6969954))
-);
-distr_duration!(distr_uniform_duration_edge,
- Uniform::new_inclusive(Duration::new(LARGE_SEC, 999_999_999), Duration::new(LARGE_SEC + 1, 1))
-);
-
-
-// standard
-distr_int!(distr_standard_i8, i8, Standard);
-distr_int!(distr_standard_i16, i16, Standard);
-distr_int!(distr_standard_i32, i32, Standard);
-distr_int!(distr_standard_i64, i64, Standard);
-distr_int!(distr_standard_i128, i128, Standard);
-
-distr!(distr_standard_bool, bool, Standard);
-distr!(distr_standard_alphanumeric, char, Alphanumeric);
-distr!(distr_standard_codepoint, char, Standard);
-
-distr_float!(distr_standard_f32, f32, Standard);
-distr_float!(distr_standard_f64, f64, Standard);
-distr_float!(distr_open01_f32, f32, Open01);
-distr_float!(distr_open01_f64, f64, Open01);
-distr_float!(distr_openclosed01_f32, f32, OpenClosed01);
-distr_float!(distr_openclosed01_f64, f64, OpenClosed01);
-
-// distributions
-distr_float!(distr_exp, f64, Exp::new(1.23 * 4.56));
-distr_float!(distr_normal, f64, Normal::new(-1.23, 4.56));
-distr_float!(distr_log_normal, f64, LogNormal::new(-1.23, 4.56));
-distr_float!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0));
-distr_float!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0));
-distr_float!(distr_cauchy, f64, Cauchy::new(4.2, 6.9));
-distr_int!(distr_binomial, u64, Binomial::new(20, 0.7));
-distr_int!(distr_poisson, u64, Poisson::new(4.0));
-distr!(distr_bernoulli, bool, Bernoulli::new(0.18));
-distr_arr!(distr_circle, [f64; 2], UnitCircle::new());
-distr_arr!(distr_sphere_surface, [f64; 3], UnitSphereSurface::new());
-
-// Weighted
-distr_int!(distr_weighted_i8, usize, WeightedIndex::new(&[1i8, 2, 3, 4, 12, 0, 2, 1]).unwrap());
-distr_int!(distr_weighted_u32, usize, WeightedIndex::new(&[1u32, 2, 3, 4, 12, 0, 2, 1]).unwrap());
-distr_int!(distr_weighted_f64, usize, WeightedIndex::new(&[1.0f64, 0.001, 1.0/3.0, 4.01, 0.0, 3.3, 22.0, 0.001]).unwrap());
-distr_int!(distr_weighted_large_set, usize, WeightedIndex::new((0..10000).rev().chain(1..10001)).unwrap());
-
-// construct and sample from a range
-macro_rules! gen_range_int {
- ($fnn:ident, $ty:ident, $low:expr, $high:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
-
- b.iter(|| {
- let mut high = $high;
- let mut accum: $ty = 0;
- for _ in 0..::RAND_BENCH_N {
- accum = accum.wrapping_add(rng.gen_range($low, high));
- // force recalculation of range each time
- high = high.wrapping_add(1) & std::$ty::MAX;
- }
- accum
- });
- b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-gen_range_int!(gen_range_i8, i8, -20i8, 100);
-gen_range_int!(gen_range_i16, i16, -500i16, 2000);
-gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000);
-gen_range_int!(gen_range_i64, i64, 3i64, 123_456_789_123);
-gen_range_int!(gen_range_i128, i128, -12345678901234i128, 123_456_789_123_456_789);
-
-// construct and sample from a floating-point range
-macro_rules! gen_range_float {
- ($fnn:ident, $ty:ident, $low:expr, $high:expr) => {
- #[bench]
- fn $fnn(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
-
- b.iter(|| {
- let mut high = $high;
- let mut low = $low;
- let mut accum: $ty = 0.0;
- for _ in 0..::RAND_BENCH_N {
- accum += rng.gen_range(low, high);
- // force recalculation of range each time
- low += 0.9;
- high += 1.1;
- }
- accum
- });
- b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
- }
- }
-}
-
-gen_range_float!(gen_range_f32, f32, -20000.0f32, 100000.0);
-gen_range_float!(gen_range_f64, f64, 123.456f64, 7890.12);
-
-#[bench]
-fn dist_iter(b: &mut Bencher) {
- let mut rng = SmallRng::from_entropy();
- let distr = Normal::new(-2.71828, 3.14159);
- let mut iter = distr.sample_iter(&mut rng);
-
- b.iter(|| {
- let mut accum = 0.0;
- for _ in 0..::RAND_BENCH_N {
- accum += iter.next().unwrap();
- }
- accum
- });
- b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
-}
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 {
diff --git a/rand/benches/misc.rs b/rand/benches/misc.rs
index 8fb3a83..4098686 100644
--- a/rand/benches/misc.rs
+++ b/rand/benches/misc.rs
@@ -9,20 +9,21 @@
#![feature(test)]
extern crate test;
-extern crate rand;
const RAND_BENCH_N: u64 = 1000;
use test::Bencher;
use rand::prelude::*;
+use rand::distributions::{Distribution, Standard, Bernoulli};
+use rand_pcg::{Pcg32, Pcg64Mcg};
#[bench]
fn misc_gen_bool_const(b: &mut Bencher) {
- let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
- for _ in 0..::RAND_BENCH_N {
+ for _ in 0..crate::RAND_BENCH_N {
accum ^= rng.gen_bool(0.18);
}
accum
@@ -31,11 +32,11 @@ fn misc_gen_bool_const(b: &mut Bencher) {
#[bench]
fn misc_gen_bool_var(b: &mut Bencher) {
- let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
let mut p = 0.18;
- for _ in 0..::RAND_BENCH_N {
+ for _ in 0..crate::RAND_BENCH_N {
accum ^= rng.gen_bool(p);
p += 0.0001;
}
@@ -45,10 +46,10 @@ fn misc_gen_bool_var(b: &mut Bencher) {
#[bench]
fn misc_gen_ratio_const(b: &mut Bencher) {
- let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
- for _ in 0..::RAND_BENCH_N {
+ for _ in 0..crate::RAND_BENCH_N {
accum ^= rng.gen_ratio(2, 3);
}
accum
@@ -57,10 +58,10 @@ fn misc_gen_ratio_const(b: &mut Bencher) {
#[bench]
fn misc_gen_ratio_var(b: &mut Bencher) {
- let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
- for i in 2..(::RAND_BENCH_N as u32 + 2) {
+ for i in 2..(crate::RAND_BENCH_N as u32 + 2) {
accum ^= rng.gen_ratio(i, i + 1);
}
accum
@@ -69,11 +70,11 @@ fn misc_gen_ratio_var(b: &mut Bencher) {
#[bench]
fn misc_bernoulli_const(b: &mut Bencher) {
- let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
- let d = rand::distributions::Bernoulli::new(0.18);
+ let d = rand::distributions::Bernoulli::new(0.18).unwrap();
let mut accum = true;
- for _ in 0..::RAND_BENCH_N {
+ for _ in 0..crate::RAND_BENCH_N {
accum ^= rng.sample(d);
}
accum
@@ -82,12 +83,12 @@ fn misc_bernoulli_const(b: &mut Bencher) {
#[bench]
fn misc_bernoulli_var(b: &mut Bencher) {
- let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
let mut p = 0.18;
- for _ in 0..::RAND_BENCH_N {
- let d = rand::distributions::Bernoulli::new(p);
+ for _ in 0..crate::RAND_BENCH_N {
+ let d = Bernoulli::new(p).unwrap();
accum ^= rng.sample(d);
p += 0.0001;
}
@@ -95,30 +96,10 @@ fn misc_bernoulli_var(b: &mut Bencher) {
})
}
-macro_rules! sample_binomial {
- ($name:ident, $n:expr, $p:expr) => {
- #[bench]
- fn $name(b: &mut Bencher) {
- let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
- let (n, p) = ($n, $p);
- b.iter(|| {
- let d = rand::distributions::Binomial::new(n, p);
- rng.sample(d)
- })
- }
- }
-}
-
-sample_binomial!(misc_binomial_1, 1, 0.9);
-sample_binomial!(misc_binomial_10, 10, 0.9);
-sample_binomial!(misc_binomial_100, 100, 0.99);
-sample_binomial!(misc_binomial_1000, 1000, 0.01);
-sample_binomial!(misc_binomial_1e12, 1000_000_000_000, 0.2);
-
#[bench]
fn gen_1k_iter_repeat(b: &mut Bencher) {
use std::iter;
- let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = iter::repeat(()).map(|()| rng.gen()).take(128).collect();
v
@@ -128,8 +109,7 @@ fn gen_1k_iter_repeat(b: &mut Bencher) {
#[bench]
fn gen_1k_sample_iter(b: &mut Bencher) {
- use rand::distributions::{Distribution, Standard};
- let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
v
@@ -139,7 +119,7 @@ fn gen_1k_sample_iter(b: &mut Bencher) {
#[bench]
fn gen_1k_gen_array(b: &mut Bencher) {
- let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
// max supported array length is 32!
let v: [[u64; 32]; 4] = rng.gen();
@@ -150,7 +130,7 @@ fn gen_1k_gen_array(b: &mut Bencher) {
#[bench]
fn gen_1k_fill(b: &mut Bencher) {
- let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
+ let mut rng = Pcg64Mcg::from_rng(&mut thread_rng()).unwrap();
let mut buf = [0u64; 128];
b.iter(|| {
rng.fill(&mut buf[..]);
diff --git a/rand/benches/seq.rs b/rand/benches/seq.rs
index 0ca3398..4c671b8 100644
--- a/rand/benches/seq.rs
+++ b/rand/benches/seq.rs
@@ -10,7 +10,6 @@
#![allow(non_snake_case)]
extern crate test;
-extern crate rand;
use test::Bencher;
@@ -18,6 +17,10 @@ use rand::prelude::*;
use rand::seq::*;
use std::mem::size_of;
+// We force use of 32-bit RNG since seq code is optimised for use with 32-bit
+// generators on all platforms.
+use rand_pcg::Pcg32 as SmallRng;
+
const RAND_BENCH_N: u64 = 1000;
#[bench]
@@ -44,7 +47,7 @@ fn seq_slice_choose_1_of_1000(b: &mut Bencher) {
}
s
});
- b.bytes = size_of::<usize>() as u64 * ::RAND_BENCH_N;
+ b.bytes = size_of::<usize>() as u64 * crate::RAND_BENCH_N;
}
macro_rules! seq_slice_choose_multiple {
@@ -86,7 +89,7 @@ fn seq_iter_choose_from_1000(b: &mut Bencher) {
}
s
});
- b.bytes = size_of::<usize>() as u64 * ::RAND_BENCH_N;
+ b.bytes = size_of::<usize>() as u64 * crate::RAND_BENCH_N;
}
#[derive(Clone)]
diff --git a/rand/benches/weighted.rs b/rand/benches/weighted.rs
new file mode 100644
index 0000000..5ddca3f
--- /dev/null
+++ b/rand/benches/weighted.rs
@@ -0,0 +1,36 @@
+// Copyright 2019 Developers of the Rand project.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![feature(test)]
+
+extern crate test;
+
+use test::Bencher;
+use rand::Rng;
+use rand::distributions::WeightedIndex;
+
+#[bench]
+fn weighted_index_creation(b: &mut Bencher) {
+ let mut rng = rand::thread_rng();
+ let weights = [1u32, 2, 4, 0, 5, 1, 7, 1, 2, 3, 4, 5, 6, 7];
+ b.iter(|| {
+ let distr = WeightedIndex::new(weights.to_vec()).unwrap();
+ rng.sample(distr)
+ })
+}
+
+#[bench]
+fn weighted_index_modification(b: &mut Bencher) {
+ let mut rng = rand::thread_rng();
+ let weights = [1u32, 2, 3, 0, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7];
+ let mut distr = WeightedIndex::new(weights.to_vec()).unwrap();
+ b.iter(|| {
+ distr.update_weights(&[(2, &4), (5, &1)]).unwrap();
+ rng.sample(&distr)
+ })
+}