Skip to content

Commit

Permalink
Add benchmarks for gen_bool and SmallRng
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 20, 2018
1 parent 00b7616 commit 657f86e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 5 additions & 1 deletion benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const BYTES_LEN: usize = 1024;
use std::mem::size_of;
use test::{black_box, Bencher};

use rand::{RngCore, Rng, SeedableRng, NewRng, StdRng, OsRng, JitterRng, EntropyRng};
use rand::{RngCore, Rng, SeedableRng, NewRng};
use rand::{StdRng, SmallRng, OsRng, JitterRng, EntropyRng};
use rand::{XorShiftRng, Hc128Rng, IsaacRng, Isaac64Rng, ChaChaRng};
use rand::reseeding::ReseedingRng;
use rand::prng::hc128::Hc128Core;
Expand Down Expand Up @@ -37,6 +38,7 @@ gen_bytes!(gen_bytes_hc128, Hc128Rng::new());
gen_bytes!(gen_bytes_isaac, IsaacRng::new());
gen_bytes!(gen_bytes_isaac64, Isaac64Rng::new());
gen_bytes!(gen_bytes_std, StdRng::new());
gen_bytes!(gen_bytes_small, SmallRng::new());
gen_bytes!(gen_bytes_os, OsRng::new().unwrap());

macro_rules! gen_uint {
Expand All @@ -59,13 +61,15 @@ gen_uint!(gen_u32_hc128, u32, Hc128Rng::new());
gen_uint!(gen_u32_isaac, u32, IsaacRng::new());
gen_uint!(gen_u32_isaac64, u32, Isaac64Rng::new());
gen_uint!(gen_u32_std, u32, StdRng::new());
gen_uint!(gen_u32_small, u32, SmallRng::new());
gen_uint!(gen_u32_os, u32, OsRng::new().unwrap());

gen_uint!(gen_u64_xorshift, u64, XorShiftRng::new());
gen_uint!(gen_u64_hc128, u64, Hc128Rng::new());
gen_uint!(gen_u64_isaac, u64, IsaacRng::new());
gen_uint!(gen_u64_isaac64, u64, Isaac64Rng::new());
gen_uint!(gen_u64_std, u64, StdRng::new());
gen_uint!(gen_u64_small, u64, SmallRng::new());
gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());

// Do not test JitterRng like the others by running it RAND_BENCH_N times per,
Expand Down
24 changes: 24 additions & 0 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@
extern crate test;
extern crate rand;

const RAND_BENCH_N: u64 = 1000;

use test::{black_box, Bencher};

use rand::{SeedableRng, SmallRng, Rng, thread_rng};
use rand::seq::*;

#[bench]
fn misc_gen_bool(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
for _ in 0..::RAND_BENCH_N {
black_box(rng.gen_bool(0.18));
}
})
}

#[bench]
fn misc_gen_bool_var(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
for _ in 0..::RAND_BENCH_N {
let mut p = 0.18;
black_box(rng.gen_bool(p));
black_box(p);
}
})
}

#[bench]
fn misc_shuffle_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ impl<R: SeedableRng> NewRng for R {
}
}

/// The standard RNG. The PRNG algorithm in `StdRng` is choosen to be efficient
/// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient
/// on the current platform, to be statistically strong and unpredictable
/// (meaning a cryptographically secure PRNG).
///
Expand Down Expand Up @@ -865,7 +865,7 @@ impl SeedableRng for StdRng {
}

/// An RNG recommended when small state, cheap initialization and good
/// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
/// performance are required. The PRNG algorithm in `SmallRng` is chosen to be
/// efficient on the current platform, **without consideration for cryptography
/// or security**. The size of its state is much smaller than for `StdRng`.
///
Expand Down Expand Up @@ -908,10 +908,12 @@ impl SeedableRng for StdRng {
pub struct SmallRng(XorShiftRng);

impl RngCore for SmallRng {
#[inline(always)]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}

#[inline(always)]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
Expand Down

0 comments on commit 657f86e

Please sign in to comment.