Skip to content

Commit

Permalink
temp: implement enough of CryptoRng<E> for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Aug 21, 2017
1 parent 2a59377 commit facd4ae
Show file tree
Hide file tree
Showing 20 changed files with 298 additions and 208 deletions.
20 changes: 10 additions & 10 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use rand::distributions::gamma::Gamma;

#[bench]
fn distr_baseline(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();

b.iter(|| {
for _ in 0..::RAND_BENCH_N {
Expand All @@ -32,7 +32,7 @@ fn distr_baseline(b: &mut Bencher) {

#[bench]
fn distr_range_int(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = Range::new(3i64, 134217671i64);

b.iter(|| {
Expand All @@ -45,7 +45,7 @@ fn distr_range_int(b: &mut Bencher) {

#[bench]
fn distr_range_float(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = Range::new(2.26f64, 2.319f64);

b.iter(|| {
Expand All @@ -59,7 +59,7 @@ fn distr_range_float(b: &mut Bencher) {

#[bench]
fn distr_range2_int(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = range2::range(3i64, 134217671i64);

b.iter(|| {
Expand All @@ -72,7 +72,7 @@ fn distr_range2_int(b: &mut Bencher) {

#[bench]
fn distr_range2_float(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = range2::range(2.26f64, 2.319f64);

b.iter(|| {
Expand All @@ -86,7 +86,7 @@ fn distr_range2_float(b: &mut Bencher) {

#[bench]
fn distr_exp(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = Exp::new(2.71828 * 3.14159);

b.iter(|| {
Expand All @@ -100,7 +100,7 @@ fn distr_exp(b: &mut Bencher) {

#[bench]
fn distr_normal(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = Normal::new(-2.71828, 3.14159);

b.iter(|| {
Expand All @@ -113,7 +113,7 @@ fn distr_normal(b: &mut Bencher) {

#[bench]
fn distr_log_normal(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = LogNormal::new(-2.71828, 3.14159);

b.iter(|| {
Expand All @@ -127,7 +127,7 @@ fn distr_log_normal(b: &mut Bencher) {

#[bench]
fn distr_gamma_large_shape(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = Gamma::new(10., 1.0);

b.iter(|| {
Expand All @@ -140,7 +140,7 @@ fn distr_gamma_large_shape(b: &mut Bencher) {

#[bench]
fn distr_gamma_small_shape(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let mut rng = XorShiftRng::new().unwrap();
let distr = Gamma::new(0.1, 1.0);

b.iter(|| {
Expand Down
20 changes: 10 additions & 10 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ fn gen_usize_std(b: &mut Bencher) {
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}

#[bench]
fn gen_usize_os(b: &mut Bencher) {
let mut rng = OsRng::new().unwrap();
b.iter(|| {
for _ in 0..RAND_BENCH_N {
black_box(usize::rand(&mut rng, Default));
}
});
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}
// #[bench]
// fn gen_usize_os(b: &mut Bencher) {
// let mut rng = OsRng::new().unwrap();
// b.iter(|| {
// for _ in 0..RAND_BENCH_N {
// black_box(usize::rand(&mut rng, Default));
// }
// });
// b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
// }
36 changes: 18 additions & 18 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::mem::size_of;
use test::{black_box, Bencher};
use rand::StdRng;
use rand::prng::XorShiftRng;
use rand::sequences::{sample, Shuffle};
// use rand::sequences::{sample, Shuffle};
use rand::distributions::{Rand, Uniform, Uniform01};

#[bench]
Expand Down Expand Up @@ -56,20 +56,20 @@ fn misc_convert_f64(b: &mut Bencher) {
b.bytes = size_of::<f64>() as u64 * RAND_BENCH_N;
}

#[bench]
fn misc_shuffle_100(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let x : &mut [usize] = &mut [1; 100];
b.iter(|| {
x.shuffle(&mut rng);
})
}

#[bench]
fn misc_sample_10_of_100(b: &mut Bencher) {
let mut rng = XorShiftRng::new();
let x : &[usize] = &[1; 100];
b.iter(|| {
sample(&mut rng, x, 10);
})
}
// #[bench]
// fn misc_shuffle_100(b: &mut Bencher) {
// let mut rng = XorShiftRng::new();
// let x : &mut [usize] = &mut [1; 100];
// b.iter(|| {
// x.shuffle(&mut rng);
// })
// }
//
// #[bench]
// fn misc_sample_10_of_100(b: &mut Bencher) {
// let mut rng = XorShiftRng::new();
// let x : &[usize] = &[1; 100];
// b.iter(|| {
// sample(&mut rng, x, 10);
// })
// }
3 changes: 2 additions & 1 deletion src/distributions/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Distribution<char> for Default {
}
}


/*
#[cfg(test)]
mod tests {
use {Rng, thread_rng};
Expand All @@ -85,3 +85,4 @@ mod tests {
do_test::<bool>(rng);
}
}
*/
3 changes: 2 additions & 1 deletion src/distributions/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Distribution<f64> for Exp {
exp1(rng) * self.lambda_inverse
}
}

/*
#[cfg(test)]
mod test {
use distributions::{Distribution};
Expand All @@ -113,3 +113,4 @@ mod test {
Exp::new(-10.0);
}
}
*/
3 changes: 2 additions & 1 deletion src/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ impl Distribution<f64> for StudentT {
norm * (self.dof / self.chi.sample(rng)).sqrt()
}
}

/*
#[cfg(test)]
mod test {
use distributions::{Distribution};
Expand Down Expand Up @@ -361,3 +361,4 @@ mod test {
}
}
}
*/
5 changes: 3 additions & 2 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Rng;

pub use self::default::Default;
pub use self::uniform::{uniform, codepoint, ascii_word_char};
pub use self::uniform::{uniform, codepoint/*, ascii_word_char*/};
pub use self::uniform::{Uniform, Uniform01, Open01, Closed01, AsciiWordChar};
pub use self::range::{Range};

Expand Down Expand Up @@ -197,7 +197,7 @@ fn ziggurat<R: Rng+?Sized, P, Z>(
}
}
}

/*
#[cfg(test)]
mod test {
use {Rng, thread_rng};
Expand All @@ -211,3 +211,4 @@ mod test {
assert_eq!(weighted_bool(1, s), true);
}
}
*/
3 changes: 2 additions & 1 deletion src/distributions/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl Distribution<f64> for LogNormal {
self.norm.sample(rng).exp()
}
}

/*
#[cfg(test)]
mod tests {
use distributions::{Distribution};
Expand Down Expand Up @@ -185,3 +185,4 @@ mod tests {
LogNormal::new(10.0, -1.0);
}
}
*/
3 changes: 2 additions & 1 deletion src/distributions/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ macro_rules! float_impl {

float_impl! { f32 }
float_impl! { f64 }

/*
#[cfg(test)]
mod tests {
use {Rng, thread_rng};
Expand Down Expand Up @@ -308,3 +308,4 @@ mod tests {
}
}
}
*/
3 changes: 2 additions & 1 deletion src/distributions/range2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ macro_rules! range_float_impl {

range_float_impl! { f32 }
range_float_impl! { f64 }

/*
#[cfg(test)]
mod tests {
use {Rng, thread_rng};
Expand Down Expand Up @@ -342,3 +342,4 @@ mod tests {
}
}
}
*/
11 changes: 6 additions & 5 deletions src/distributions/uniform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub fn codepoint<R: Rng+?Sized>(rng: &mut R) -> char {
}
}
}

/*
/// Sample a `char`, uniformly distributed over ASCII letters and numbers:
/// a-z, A-Z and 0-9.
///
Expand All @@ -60,7 +60,7 @@ pub fn ascii_word_char<R: Rng+?Sized>(rng: &mut R) -> char {
0123456789";
*GEN_ASCII_STR_CHARSET.choose(rng).unwrap() as char
}

*/

// ----- Sampling distributions -----

Expand Down Expand Up @@ -249,14 +249,14 @@ macro_rules! float_impls {
}
float_impls! { SCALE_F64, f64, 53 }
float_impls! { SCALE_F32, f32, 24 }

/*
impl Distribution<char> for AsciiWordChar {
fn sample<R: Rng+?Sized>(&self, rng: &mut R) -> char {
ascii_word_char(rng)
}
}


*/
/*
#[cfg(test)]
mod tests {
use {ConstRng, thread_rng, iter};
Expand Down Expand Up @@ -345,3 +345,4 @@ mod tests {
}
}
}
*/
3 changes: 2 additions & 1 deletion src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'a, R:?Sized+'a, U, F> Iterator for FlatMap<'a, R, U, F>
self.len.map_or((usize::MAX, None), |len| (len, Some(len)))
}
}

/*
#[cfg(test)]
mod tests {
use {Rng, thread_rng, iter};
Expand Down Expand Up @@ -178,3 +178,4 @@ mod tests {
assert_eq!(x.len(), 10);
}
}
*/
Loading

0 comments on commit facd4ae

Please sign in to comment.