Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NewSeeded: rename new → try_new, add new_with_fallback #21

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ClockRng: export and add benches
  • Loading branch information
dhardy committed Oct 24, 2017
commit 07a350a67a3c5e61461c9b3b9f7602121dff27ea
45 changes: 28 additions & 17 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const BYTES_LEN: usize = 1024;
use std::mem::size_of;
use test::{black_box, Bencher};

use rand::{Rng, NewSeeded, SeedFromRng, StdRng, OsRng, Rand, Default};
use rand::{Rng, NewSeeded, SeedFromRng, StdRng, ClockRng, OsRng, Rand, Default};
use rand::prng::{XorShiftRng, IsaacRng, Isaac64Rng, ChaChaRng};

macro_rules! gen_bytes {
($fnn:ident, $gen:ident) => {
($fnn:ident, $gen:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = $gen::try_new().unwrap();
let mut rng = $gen;
let mut buf = [0u8; BYTES_LEN];
b.iter(|| {
for _ in 0..RAND_BENCH_N {
Expand All @@ -29,19 +29,19 @@ macro_rules! gen_bytes {
}
}

gen_bytes!(gen_bytes_xorshift, XorShiftRng);
gen_bytes!(gen_bytes_isaac, IsaacRng);
gen_bytes!(gen_bytes_isaac64, Isaac64Rng);
gen_bytes!(gen_bytes_chacha, ChaChaRng);
gen_bytes!(gen_bytes_std, StdRng);
gen_bytes!(gen_bytes_os, OsRng);
gen_bytes!(gen_bytes_xorshift, XorShiftRng::try_new().unwrap());
gen_bytes!(gen_bytes_isaac, IsaacRng::try_new().unwrap());
gen_bytes!(gen_bytes_isaac64, Isaac64Rng::try_new().unwrap());
gen_bytes!(gen_bytes_chacha, ChaChaRng::try_new().unwrap());
gen_bytes!(gen_bytes_std, StdRng::try_new().unwrap());
gen_bytes!(gen_bytes_clock, ClockRng::new());


macro_rules! gen_usize {
($fnn:ident, $gen:ident) => {
($fnn:ident, $gen:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = $gen::try_new().unwrap();
let mut rng = $gen;
b.iter(|| {
for _ in 0..RAND_BENCH_N {
black_box(usize::rand(&mut rng, Default));
Expand All @@ -52,12 +52,13 @@ macro_rules! gen_usize {
}
}

gen_usize!(gen_usize_xorshift, XorShiftRng);
gen_usize!(gen_usize_isaac, IsaacRng);
gen_usize!(gen_usize_isaac64, Isaac64Rng);
gen_usize!(gen_usize_chacha, ChaChaRng);
gen_usize!(gen_usize_std, StdRng);
gen_usize!(gen_usize_os, OsRng);
gen_usize!(gen_usize_xorshift, XorShiftRng::try_new().unwrap());
gen_usize!(gen_usize_isaac, IsaacRng::try_new().unwrap());
gen_usize!(gen_usize_isaac64, Isaac64Rng::try_new().unwrap());
gen_usize!(gen_usize_chacha, ChaChaRng::try_new().unwrap());
gen_usize!(gen_usize_std, StdRng::try_new().unwrap());
gen_usize!(gen_usize_clock, ClockRng::new());
gen_usize!(gen_usize_os, OsRng::try_new().unwrap());

macro_rules! init_gen {
($fnn:ident, $gen:ident) => {
Expand All @@ -78,3 +79,13 @@ init_gen!(init_isaac, IsaacRng);
init_gen!(init_isaac64, Isaac64Rng);
init_gen!(init_chacha, ChaChaRng);
init_gen!(init_std, StdRng);

// Differs from above in that it doesn't have a seeding rng
#[bench]
fn init_clock(b: &mut Bencher) {
b.iter(|| {
for _ in 0..RAND_BENCH_N {
black_box(ClockRng::new());
}
});
}
1 change: 1 addition & 0 deletions src/clock_rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use core::num::Wrapping as w;
/// The time is checked once per `u32` extracted and mixed into the current
/// state via a RNG, hence in theory long output sequences will contain slightly
/// more entropy than short ones.
#[derive(Debug)]
pub struct ClockRng {
state: w<u64>,
}
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ extern crate rand_core;

pub use rand_core::{Rng, CryptoRng, SeedFromRng, SeedableRng, Error, ErrorKind};

pub use clock_rng::ClockRng;
#[cfg(feature="std")]
pub use read::ReadRng;
#[cfg(feature="std")]
Expand All @@ -269,8 +270,6 @@ pub use thread_local::{ThreadRng, thread_rng, random, random_with};
use prng::IsaacWordRng;
use distributions::range::Range;

// TODO: should we export this?
#[cfg(feature="std")] // not needed otherwise
mod clock_rng;

pub mod distributions;
Expand Down