-
Notifications
You must be signed in to change notification settings - Fork 431
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
Clean up benchmarks #465
Clean up benchmarks #465
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -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. | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case I think the point was to check what affect using an unknown There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is also possible to benchmark, but then we should expect the benchmark to give the same result as the const version. Does not seem like something all that useful to measure to me. |
||
accum ^= rng.sample(d); | ||
p += 0.0001; | ||
} | ||
accum | ||
}) | ||
} | ||
|
||
|
@@ -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) | ||
}) | ||
} | ||
} | ||
|
@@ -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] | ||
}) | ||
} | ||
|
||
|
@@ -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) | ||
}) | ||
} | ||
|
||
|
@@ -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) | ||
}) | ||
} | ||
|
||
|
@@ -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) | ||
}) | ||
} | ||
|
||
|
@@ -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) | ||
}) | ||
} | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} | ||
|
@@ -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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjusting p seems sensible, but it adds an extra op, making this incomparable with
misc_gen_bool_const
. Was the old method not okay?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although LLVM didn't move the range set-up code out of the inner loop, I see no reason why it couldn't. Changing
p
makes sure it can not.Funny thing: using
black_box
before the for loop is slower than incrementingp
each round.