Skip to content

Commit

Permalink
Clean up benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed May 19, 2018
1 parent 5953334 commit 9263b5c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
15 changes: 9 additions & 6 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern crate rand;
const RAND_BENCH_N: u64 = 1000;

use std::mem::size_of;
use test::{black_box, Bencher};
use test::Bencher;

use rand::{Rng, FromEntropy, XorShiftRng};
use rand::distributions::*;
Expand All @@ -26,7 +26,7 @@ macro_rules! distr_int {
let x: $ty = distr.sample(&mut rng);
accum = accum.wrapping_add(x);
}
black_box(accum);
accum
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
Expand All @@ -46,7 +46,7 @@ macro_rules! distr_float {
let x: $ty = distr.sample(&mut rng);
accum += x;
}
black_box(accum);
accum
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
Expand All @@ -61,10 +61,12 @@ macro_rules! distr {
let distr = $distr;

b.iter(|| {
let mut accum = 0u32;
for _ in 0..::RAND_BENCH_N {
let x: $ty = distr.sample(&mut rng);
black_box(x);
accum = accum.wrapping_add(x as u32);
}
accum
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
Expand Down Expand Up @@ -109,6 +111,7 @@ 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_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));


// construct and sample from a range
Expand All @@ -126,7 +129,7 @@ macro_rules! gen_range_int {
// force recalculation of range each time
high = high.wrapping_add(1) & std::$ty::MAX;
}
black_box(accum);
accum
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
Expand All @@ -151,7 +154,7 @@ fn dist_iter(b: &mut Bencher) {
for _ in 0..::RAND_BENCH_N {
accum += iter.next().unwrap();
}
black_box(accum);
accum
});
b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
}
12 changes: 6 additions & 6 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ macro_rules! gen_uint {
for _ in 0..RAND_BENCH_N {
accum = accum.wrapping_add(rng.gen::<$ty>());
}
black_box(accum);
accum
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());
fn gen_u64_jitter(b: &mut Bencher) {
let mut rng = JitterRng::new().unwrap();
b.iter(|| {
black_box(rng.gen::<u64>());
rng.gen::<u64>()
});
b.bytes = size_of::<u64>() as u64;
}
Expand All @@ -94,7 +94,7 @@ macro_rules! init_gen {
let mut rng = XorShiftRng::from_entropy();
b.iter(|| {
let r2 = $gen::from_rng(&mut rng).unwrap();
black_box(r2);
r2
});
}
}
Expand All @@ -109,7 +109,7 @@ init_gen!(init_chacha, ChaChaRng);
#[bench]
fn init_jitter(b: &mut Bencher) {
b.iter(|| {
black_box(JitterRng::new().unwrap());
JitterRng::new().unwrap()
});
}

Expand Down Expand Up @@ -144,7 +144,7 @@ macro_rules! reseeding_uint {
for _ in 0..RAND_BENCH_N {
accum = accum.wrapping_add(rng.gen::<$ty>());
}
black_box(accum);
accum
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
Expand All @@ -165,7 +165,7 @@ macro_rules! threadrng_uint {
for _ in 0..RAND_BENCH_N {
accum = accum.wrapping_add(rng.gen::<$ty>());
}
black_box(accum);
accum
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
Expand Down
45 changes: 24 additions & 21 deletions benches/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ extern crate rand;

const RAND_BENCH_N: u64 = 1000;

use test::{black_box, Bencher};
use test::Bencher;

use rand::prelude::*;
use rand::seq::*;

#[bench]
fn misc_gen_bool_const(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
// Can be evaluated at compile time.
let mut accum = true;
for _ in 0..::RAND_BENCH_N {
accum ^= rng.gen_bool(0.18);
Expand All @@ -25,19 +24,21 @@ fn misc_gen_bool_const(b: &mut Bencher) {

#[bench]
fn misc_gen_bool_var(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
let mut p = 0.18;
black_box(&mut p); // Avoid constant folding.
for _ in 0..::RAND_BENCH_N {
black_box(rng.gen_bool(p));
accum ^= rng.gen_bool(p);
p += 0.0001;
}
accum
})
}

#[bench]
fn misc_bernoulli_const(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
let d = rand::distributions::Bernoulli::new(0.18);
b.iter(|| {
// Can be evaluated at compile time.
Expand All @@ -51,14 +52,16 @@ fn misc_bernoulli_const(b: &mut Bencher) {

#[bench]
fn misc_bernoulli_var(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
let mut rng = StdRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
let mut p = 0.18;
black_box(&mut p); // Avoid constant folding.
let d = rand::distributions::Bernoulli::new(p);
for _ in 0..::RAND_BENCH_N {
black_box(rng.sample(d));
let d = rand::distributions::Bernoulli::new(p);
accum ^= rng.sample(d);
p += 0.0001;
}
accum
})
}

Expand All @@ -70,7 +73,7 @@ macro_rules! sample_binomial {
let (n, p) = ($n, $p);
b.iter(|| {
let d = rand::distributions::Binomial::new(n, p);
black_box(rng.sample(d));
rng.sample(d)
})
}
}
Expand All @@ -88,7 +91,7 @@ fn misc_shuffle_100(b: &mut Bencher) {
let x : &mut [usize] = &mut [1; 100];
b.iter(|| {
rng.shuffle(x);
black_box(&x);
x[0]
})
}

Expand All @@ -97,7 +100,7 @@ fn misc_sample_iter_10_of_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
black_box(sample_iter(&mut rng, x, 10).unwrap_or_else(|e| e));
sample_iter(&mut rng, x, 10).unwrap_or_else(|e| e)
})
}

Expand All @@ -106,7 +109,7 @@ fn misc_sample_slice_10_of_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
black_box(sample_slice(&mut rng, x, 10));
sample_slice(&mut rng, x, 10)
})
}

Expand All @@ -115,7 +118,7 @@ fn misc_sample_slice_ref_10_of_100(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
let x : &[usize] = &[1; 100];
b.iter(|| {
black_box(sample_slice_ref(&mut rng, x, 10));
sample_slice_ref(&mut rng, x, 10)
})
}

Expand All @@ -125,7 +128,7 @@ macro_rules! sample_indices {
fn $name(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(thread_rng()).unwrap();
b.iter(|| {
black_box(sample_indices(&mut rng, $length, $amount));
sample_indices(&mut rng, $length, $amount)
})
}
}
Expand All @@ -141,7 +144,7 @@ fn gen_1k_iter_repeat(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = iter::repeat(()).map(|()| rng.gen()).take(128).collect();
black_box(v);
v
});
b.bytes = 1024;
}
Expand All @@ -152,7 +155,7 @@ fn gen_1k_gen_iter(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = rng.gen_iter().take(128).collect();
black_box(v);
v
});
b.bytes = 1024;
}
Expand All @@ -163,7 +166,7 @@ fn gen_1k_sample_iter(b: &mut Bencher) {
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let v: Vec<u64> = Standard.sample_iter(&mut rng).take(128).collect();
black_box(v);
v
});
b.bytes = 1024;
}
Expand All @@ -174,7 +177,7 @@ fn gen_1k_fill(b: &mut Bencher) {
let mut buf = [0u64; 128];
b.iter(|| {
rng.fill(&mut buf[..]);
black_box(buf);
buf
});
b.bytes = 1024;
}

0 comments on commit 9263b5c

Please sign in to comment.