Skip to content

Commit

Permalink
Merge pull request #317 from dhardy/doc
Browse files Browse the repository at this point in the history
homepage, bench improvement and doc fix
  • Loading branch information
pitdicker authored Mar 21, 2018
2 parents e08612c + a76487c commit 4634912
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-lang-nursery/rand"
documentation = "https://docs.rs/rand"
homepage = "https://github.com/rust-lang-nursery/rand"
homepage = "https://crates.io/crates/rand"
description = """
Random number generators and other randomness functionality.
"""
Expand Down
96 changes: 69 additions & 27 deletions benches/distributions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,46 @@ use test::{black_box, Bencher};
use rand::{Rng, NewRng, XorShiftRng};
use rand::distributions::*;

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

b.iter(|| {
let mut accum = 0 as $ty;
for _ in 0..::RAND_BENCH_N {
let x: $ty = distr.sample(&mut rng);
accum = accum.wrapping_add(x);
}
black_box(accum);
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
}
}

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

b.iter(|| {
let mut accum = 0.0;
for _ in 0..::RAND_BENCH_N {
let x: $ty = distr.sample(&mut rng);
accum = accum + x;
}
black_box(accum);
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
}
}

macro_rules! distr {
($fnn:ident, $ty:ty, $distr:expr) => {
#[bench]
Expand All @@ -31,62 +71,64 @@ macro_rules! distr {
}

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

distr!(distr_range_f32, f32, Range::new(2.26f32, 2.319));
distr!(distr_range_f64, f64, Range::new(2.26f64, 2.319));
distr_float!(distr_range_f32, f32, Range::new(2.26f32, 2.319));
distr_float!(distr_range_f64, 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);
distr_int!(distr_uniform_i8, i8, Uniform);
distr_int!(distr_uniform_i16, i16, Uniform);
distr_int!(distr_uniform_i32, i32, Uniform);
distr_int!(distr_uniform_i64, i64, Uniform);
#[cfg(feature = "i128_support")]
distr!(distr_uniform_i128, i128, Uniform);
distr_int!(distr_uniform_i128, i128, Uniform);

distr!(distr_uniform_bool, bool, Uniform);
distr!(distr_uniform_alphanumeric, char, Alphanumeric);
distr!(distr_uniform_codepoint, char, Uniform);

distr!(distr_uniform_f32, f32, Uniform);
distr!(distr_uniform_f64, f64, Uniform);
distr_float!(distr_uniform_f32, f32, Uniform);
distr_float!(distr_uniform_f64, f64, Uniform);

// 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));
distr!(distr_binomial, u64, Binomial::new(20, 0.7));
distr!(distr_poisson, u64, Poisson::new(4.0));
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));
distr_int!(distr_binomial, u64, Binomial::new(20, 0.7));
distr_int!(distr_poisson, u64, Poisson::new(4.0));


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

b.iter(|| {
let mut high = $high;
let mut accum: $ty = 0;
for _ in 0..::RAND_BENCH_N {
let x: $ty = rng.gen_range($low, high);
black_box(x);
black_box(high);
accum = accum.wrapping_add(rng.gen_range($low, high));
// force recalculation of range each time
high = high.wrapping_add(1) & std::$ty::MAX;
}
black_box(accum);
});
b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
}
}
}

gen_range_int!(gen_range_i8, i8, 20i8, 100);
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);
Expand Down
20 changes: 15 additions & 5 deletions benches/generators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ macro_rules! gen_uint {
fn $fnn(b: &mut Bencher) {
let mut rng = $gen;
b.iter(|| {
let mut accum: $ty = 0;
for _ in 0..RAND_BENCH_N {
black_box(rng.gen::<$ty>());
accum = accum.wrapping_add(rng.gen::<$ty>());
}
black_box(accum);
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
Expand Down Expand Up @@ -126,9 +128,11 @@ macro_rules! chacha_rounds {
let mut rng = ChaChaRng::new();
rng.set_rounds($rounds);
b.iter(|| {
let mut accum: u32 = 0;
for _ in 0..RAND_BENCH_N {
black_box(rng.gen::<u32>());
accum = accum.wrapping_add(rng.gen::<u32>());
}
black_box(accum);
});
b.bytes = size_of::<u32>() as u64 * RAND_BENCH_N;
}
Expand All @@ -138,9 +142,11 @@ macro_rules! chacha_rounds {
let mut rng = ChaChaRng::new();
rng.set_rounds($rounds);
b.iter(|| {
let mut accum: u64 = 0;
for _ in 0..RAND_BENCH_N {
black_box(rng.gen::<u64>());
accum = accum.wrapping_add(rng.gen::<u64>());
}
black_box(accum);
});
b.bytes = size_of::<u64>() as u64 * RAND_BENCH_N;
}
Expand Down Expand Up @@ -178,9 +184,11 @@ macro_rules! reseeding_uint {
RESEEDING_THRESHOLD,
EntropyRng::new());
b.iter(|| {
let mut accum: $ty = 0;
for _ in 0..RAND_BENCH_N {
black_box(rng.gen::<$ty>());
accum = accum.wrapping_add(rng.gen::<$ty>());
}
black_box(accum);
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
Expand All @@ -197,9 +205,11 @@ macro_rules! threadrng_uint {
fn $fnn(b: &mut Bencher) {
let mut rng = thread_rng();
b.iter(|| {
let mut accum: $ty = 0;
for _ in 0..RAND_BENCH_N {
black_box(rng.gen::<$ty>());
accum = accum.wrapping_add(rng.gen::<$ty>());
}
black_box(accum);
});
b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
}
Expand Down
4 changes: 2 additions & 2 deletions rand-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ authors = ["The Rust Project Developers"]
license = "MIT/Apache-2.0"
readme = "README.md"
repository = "https://github.com/rust-lang-nursery/rand"
documentation = "https://docs.rs/rand"
homepage = "https://github.com/rust-lang-nursery/rand"
documentation = "https://docs.rs/rand-core"
homepage = "https://crates.io/crates/rand-core"
description = """
Core random number generator traits and tools for implementation.
"""
Expand Down
2 changes: 1 addition & 1 deletion rand-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ prefer to use the main [rand] crate.

[Documentation](https://docs.rs/rand-core)

[rand]: ..
[rand]: ../README.md


# License
Expand Down

0 comments on commit 4634912

Please sign in to comment.