Skip to content

Commit

Permalink
Add a few extra distr benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Dec 11, 2017
1 parent 3f22ddd commit 625f86a
Showing 1 changed file with 34 additions and 49 deletions.
83 changes: 34 additions & 49 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,12 @@ use rand::{Default, Rand, NewSeeded};
use rand::prng::XorShiftRng;
use rand::distributions::*;


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

b.iter(|| {
for _ in 0..::RAND_BENCH_N {
black_box(u64::rand(&mut rng, Default));
}
});
b.bytes = size_of::<u64>() as u64 * ::RAND_BENCH_N;
}

macro_rules! distr_range_int {
($fnn:ident, $ty:ty, $low:expr, $high:expr) => {
macro_rules! distr {
($fnn:ident, $ty:ty, $distr:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = XorShiftRng::new().unwrap();
let distr = Range::new($low, $high);
let distr = $distr;

b.iter(|| {
for _ in 0..::RAND_BENCH_N {
Expand All @@ -44,41 +31,39 @@ macro_rules! distr_range_int {
}
}

distr_range_int!(distr_range_i8, i8, 20i8, 100);
distr_range_int!(distr_range_i16, i16, -500i16, 2000);
distr_range_int!(distr_range_i32, i32, -200_000_000i32, 800_000_000);
distr_range_int!(distr_range_i64, i64, 3i64, 12345678901234);
// range
distr!(distr_range_i8, i8, Range::new(20i8, 100));
distr!(distr_range_i16, i16, Range::new(-500i16, 2000));
distr!(distr_range_i32, i32, Range::new(-200_000_000i32, 800_000_000));
distr!(distr_range_i64, i64, Range::new(3i64, 12345678901234));
#[cfg(feature = "i128_support")]
distr_range_int!(distr_range_i128, i128, -12345678901234i128, 12345678901234567890);
distr!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890));

macro_rules! distr_float {
($fnn:ident, $ty:ty, $distr:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = XorShiftRng::new().unwrap();
let distr = $distr;
distr!(distr_range_float32, f32, Range::new(2.26f32, 2.319));
distr!(distr_range_float, f64, Range::new(2.26f64, 2.319));

b.iter(|| {
for _ in 0..::RAND_BENCH_N {
let x: $ty = distr.sample(&mut rng);
black_box(x);
}
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
}
}
// uniform
distr!(distr_uniform_i8, i8, Uniform);
distr!(distr_uniform_i16, i16, Uniform);
distr!(distr_uniform_i32, i32, Uniform);
distr!(distr_uniform_i64, i64, Uniform);
#[cfg(feature = "i128_support")]
distr!(distr_uniform_i128, i128, Uniform);

distr!(distr_uniform_bool, bool, Uniform);
distr!(distr_uniform_ascii_char, char, AsciiWordChar);

distr!(distr_uniform01_float32, f32, Uniform01);
distr!(distr_closed01_float32, f32, Closed01);
distr!(distr_open01_float32, f32, Open01);

distr_float!(distr_uniform01_float32, f32, Uniform01);
distr_float!(distr_closed01_float32, f32, Closed01);
distr_float!(distr_open01_float32, f32, Open01);
distr!(distr_uniform01_float, f64, Uniform01);
distr!(distr_closed01_float, f64, Closed01);
distr!(distr_open01_float, f64, Open01);

distr_float!(distr_uniform01_float, f64, Uniform01);
distr_float!(distr_closed01_float, f64, Closed01);
distr_float!(distr_open01_float, f64, Open01);
distr_float!(distr_range_float, f64, Range::new(2.26f64, 2.319f64));
distr_float!(distr_exp, f64, Exp::new(2.71828 * 3.14159));
distr_float!(distr_normal, f64, Normal::new(-2.71828, 3.14159));
distr_float!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159));
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));
// distributions
distr!(distr_exp, f64, Exp::new(2.71828 * 3.14159));
distr!(distr_normal, f64, Normal::new(-2.71828, 3.14159));
distr!(distr_log_normal, f64, LogNormal::new(-2.71828, 3.14159));
distr!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0));
distr!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0));

0 comments on commit 625f86a

Please sign in to comment.