Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 0 additions & 34 deletions benches/bench.rs

This file was deleted.

96 changes: 96 additions & 0 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#![feature(test)]
#![cfg_attr(feature = "i128_support", feature(i128_type, i128))]

extern crate test;
extern crate rand;

const RAND_BENCH_N: u64 = 1000;

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

use rand::{Rng, NewRng, XorShiftRng};
use rand::distributions::*;

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

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;
}
}
}

// 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!(distr_range_i128, i128, Range::new(-12345678901234i128, 12345678901234567890));

distr!(distr_range_float32, f32, Range::new(2.26f32, 2.319));
distr!(distr_range_float, f64, Range::new(2.26f64, 2.319));

// 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_codepoint, char, Uniform);

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

distr!(distr_uniform01_float, f64, Uniform);
distr!(distr_closed01_float, f64, Closed01);
distr!(distr_open01_float, f64, Open01);

// 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));


// construct and sample from a range
macro_rules! gen_range_int {
($fnn:ident, $ty:ty, $low:expr, $high:expr) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = XorShiftRng::new().unwrap();
let high = $high;

b.iter(|| {
for _ in 0..::RAND_BENCH_N {
let x: $ty = rng.gen_range($low, high);
black_box(x);
black_box(high);
}
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
}
}

gen_range_int!(gen_range_i8, i8, 20i8, 100);
gen_range_int!(gen_range_i16, i16, -500i16, 2000);
gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000);
gen_range_int!(gen_range_i64, i64, 3i64, 12345678901234);
#[cfg(feature = "i128_support")]
gen_range_int!(gen_range_i128, i128, -12345678901234i128, 12345678901234567890);
18 changes: 0 additions & 18 deletions benches/distributions/exponential.rs

This file was deleted.

31 changes: 0 additions & 31 deletions benches/distributions/gamma.rs

This file was deleted.

3 changes: 0 additions & 3 deletions benches/distributions/mod.rs

This file was deleted.

18 changes: 0 additions & 18 deletions benches/distributions/normal.rs

This file was deleted.

23 changes: 4 additions & 19 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,34 +52,19 @@ macro_rules! gen_uint {
}
}

macro_rules! gen_uint_new {
($fnn:ident, $ty:ty, $gen:ident) => {
#[bench]
fn $fnn(b: &mut Bencher) {
let mut rng = $gen::new().unwrap();
b.iter(|| {
for _ in 0..RAND_BENCH_N {
black_box(rng.gen::<$ty>());
}
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
}
}

gen_uint!(gen_u32_xorshift, u32, XorShiftRng);
gen_uint!(gen_u32_hc128, u32, Hc128Rng);
gen_uint!(gen_u32_isaac, u32, IsaacRng);
gen_uint!(gen_u32_isaac64, u32, Isaac64Rng);
gen_uint_new!(gen_u32_std, u32, StdRng);
gen_uint_new!(gen_u32_os, u32, OsRng);
gen_uint!(gen_u32_std, u32, StdRng);
gen_uint!(gen_u32_os, u32, OsRng);

gen_uint!(gen_u64_xorshift, u64, XorShiftRng);
gen_uint!(gen_u64_hc128, u64, Hc128Rng);
gen_uint!(gen_u64_isaac, u64, IsaacRng);
gen_uint!(gen_u64_isaac64, u64, Isaac64Rng);
gen_uint_new!(gen_u64_std, u64, StdRng);
gen_uint_new!(gen_u64_os, u64, OsRng);
gen_uint!(gen_u64_std, u64, StdRng);
gen_uint!(gen_u64_os, u64, OsRng);

// Do not test JitterRng like the others by running it RAND_BENCH_N times per,
// measurement, because it is way too slow. Only run it once.
Expand Down