summaryrefslogtreecommitdiff
path: root/rand/benches
diff options
context:
space:
mode:
Diffstat (limited to 'rand/benches')
-rw-r--r--rand/benches/bench.rs34
-rw-r--r--rand/benches/distributions/exponential.rs18
-rw-r--r--rand/benches/distributions/gamma.rs31
-rw-r--r--rand/benches/distributions/mod.rs3
-rw-r--r--rand/benches/distributions/normal.rs18
-rw-r--r--rand/benches/generators.rs133
-rw-r--r--rand/benches/misc.rs62
7 files changed, 299 insertions, 0 deletions
diff --git a/rand/benches/bench.rs b/rand/benches/bench.rs
new file mode 100644
index 0000000..d396f25
--- /dev/null
+++ b/rand/benches/bench.rs
@@ -0,0 +1,34 @@
+#![feature(test)]
+
+extern crate test;
+extern crate rand;
+
+const RAND_BENCH_N: u64 = 1000;
+
+mod distributions;
+
+use std::mem::size_of;
+use test::{black_box, Bencher};
+use rand::{StdRng, Rng};
+
+#[bench]
+fn rand_f32(b: &mut Bencher) {
+ let mut rng = StdRng::new().unwrap();
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ black_box(rng.next_f32());
+ }
+ });
+ b.bytes = size_of::<f32>() as u64 * RAND_BENCH_N;
+}
+
+#[bench]
+fn rand_f64(b: &mut Bencher) {
+ let mut rng = StdRng::new().unwrap();
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ black_box(rng.next_f64());
+ }
+ });
+ b.bytes = size_of::<f64>() as u64 * RAND_BENCH_N;
+}
diff --git a/rand/benches/distributions/exponential.rs b/rand/benches/distributions/exponential.rs
new file mode 100644
index 0000000..152615d
--- /dev/null
+++ b/rand/benches/distributions/exponential.rs
@@ -0,0 +1,18 @@
+use std::mem::size_of;
+use test::Bencher;
+use rand;
+use rand::distributions::exponential::Exp;
+use rand::distributions::Sample;
+
+#[bench]
+fn rand_exp(b: &mut Bencher) {
+ let mut rng = rand::weak_rng();
+ let mut exp = Exp::new(2.71828 * 3.14159);
+
+ b.iter(|| {
+ for _ in 0..::RAND_BENCH_N {
+ exp.sample(&mut rng);
+ }
+ });
+ b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
+}
diff --git a/rand/benches/distributions/gamma.rs b/rand/benches/distributions/gamma.rs
new file mode 100644
index 0000000..bf3fd36
--- /dev/null
+++ b/rand/benches/distributions/gamma.rs
@@ -0,0 +1,31 @@
+use std::mem::size_of;
+use test::Bencher;
+use rand;
+use rand::distributions::IndependentSample;
+use rand::distributions::gamma::Gamma;
+
+#[bench]
+fn bench_gamma_large_shape(b: &mut Bencher) {
+ let gamma = Gamma::new(10., 1.0);
+ let mut rng = rand::weak_rng();
+
+ b.iter(|| {
+ for _ in 0..::RAND_BENCH_N {
+ gamma.ind_sample(&mut rng);
+ }
+ });
+ b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
+}
+
+#[bench]
+fn bench_gamma_small_shape(b: &mut Bencher) {
+ let gamma = Gamma::new(0.1, 1.0);
+ let mut rng = rand::weak_rng();
+
+ b.iter(|| {
+ for _ in 0..::RAND_BENCH_N {
+ gamma.ind_sample(&mut rng);
+ }
+ });
+ b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
+}
diff --git a/rand/benches/distributions/mod.rs b/rand/benches/distributions/mod.rs
new file mode 100644
index 0000000..49f6bd9
--- /dev/null
+++ b/rand/benches/distributions/mod.rs
@@ -0,0 +1,3 @@
+mod exponential;
+mod normal;
+mod gamma;
diff --git a/rand/benches/distributions/normal.rs b/rand/benches/distributions/normal.rs
new file mode 100644
index 0000000..1c858b1
--- /dev/null
+++ b/rand/benches/distributions/normal.rs
@@ -0,0 +1,18 @@
+use std::mem::size_of;
+use test::Bencher;
+use rand;
+use rand::distributions::Sample;
+use rand::distributions::normal::Normal;
+
+#[bench]
+fn rand_normal(b: &mut Bencher) {
+ let mut rng = rand::weak_rng();
+ let mut normal = Normal::new(-2.71828, 3.14159);
+
+ b.iter(|| {
+ for _ in 0..::RAND_BENCH_N {
+ normal.sample(&mut rng);
+ }
+ });
+ b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
+}
diff --git a/rand/benches/generators.rs b/rand/benches/generators.rs
new file mode 100644
index 0000000..daee7c5
--- /dev/null
+++ b/rand/benches/generators.rs
@@ -0,0 +1,133 @@
+#![feature(test)]
+
+extern crate test;
+extern crate rand;
+
+const RAND_BENCH_N: u64 = 1000;
+const BYTES_LEN: usize = 1024;
+
+use std::mem::size_of;
+use test::{black_box, Bencher};
+
+use rand::{Rng, StdRng, OsRng, JitterRng};
+use rand::{XorShiftRng, IsaacRng, Isaac64Rng, ChaChaRng};
+
+macro_rules! gen_bytes {
+ ($fnn:ident, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng: $gen = OsRng::new().unwrap().gen();
+ let mut buf = [0u8; BYTES_LEN];
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ rng.fill_bytes(&mut buf);
+ black_box(buf);
+ }
+ });
+ b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+macro_rules! gen_bytes_new {
+ ($fnn:ident, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng = $gen::new().unwrap();
+ let mut buf = [0u8; BYTES_LEN];
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ rng.fill_bytes(&mut buf);
+ black_box(buf);
+ }
+ });
+ b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+gen_bytes!(gen_bytes_xorshift, XorShiftRng);
+gen_bytes!(gen_bytes_isaac, IsaacRng);
+gen_bytes!(gen_bytes_isaac64, Isaac64Rng);
+gen_bytes!(gen_bytes_chacha, ChaChaRng);
+gen_bytes_new!(gen_bytes_std, StdRng);
+gen_bytes_new!(gen_bytes_os, OsRng);
+
+
+macro_rules! gen_uint {
+ ($fnn:ident, $ty:ty, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng: $gen = OsRng::new().unwrap().gen();
+ b.iter(|| {
+ for _ in 0..RAND_BENCH_N {
+ black_box(rng.gen::<$ty>());
+ }
+ });
+ b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
+ }
+ }
+}
+
+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_isaac, u32, IsaacRng);
+gen_uint!(gen_u32_isaac64, u32, Isaac64Rng);
+gen_uint!(gen_u32_chacha, u32, ChaChaRng);
+gen_uint_new!(gen_u32_std, u32, StdRng);
+gen_uint_new!(gen_u32_os, u32, OsRng);
+
+gen_uint!(gen_u64_xorshift, u64, XorShiftRng);
+gen_uint!(gen_u64_isaac, u64, IsaacRng);
+gen_uint!(gen_u64_isaac64, u64, Isaac64Rng);
+gen_uint!(gen_u64_chacha, u64, ChaChaRng);
+gen_uint_new!(gen_u64_std, u64, StdRng);
+gen_uint_new!(gen_u64_os, u64, OsRng);
+
+#[bench]
+fn gen_u64_jitter(b: &mut Bencher) {
+ let mut rng = JitterRng::new().unwrap();
+ b.iter(|| {
+ black_box(rng.gen::<u64>());
+ });
+ b.bytes = size_of::<u64>() as u64;
+}
+
+macro_rules! init_gen {
+ ($fnn:ident, $gen:ident) => {
+ #[bench]
+ fn $fnn(b: &mut Bencher) {
+ let mut rng: XorShiftRng = OsRng::new().unwrap().gen();
+ b.iter(|| {
+ let r2: $gen = rng.gen();
+ black_box(r2);
+ });
+ }
+ }
+}
+
+init_gen!(init_xorshift, XorShiftRng);
+init_gen!(init_isaac, IsaacRng);
+init_gen!(init_isaac64, Isaac64Rng);
+init_gen!(init_chacha, ChaChaRng);
+
+#[bench]
+fn init_jitter(b: &mut Bencher) {
+ b.iter(|| {
+ black_box(JitterRng::new().unwrap());
+ });
+}
diff --git a/rand/benches/misc.rs b/rand/benches/misc.rs
new file mode 100644
index 0000000..4251761
--- /dev/null
+++ b/rand/benches/misc.rs
@@ -0,0 +1,62 @@
+#![feature(test)]
+
+extern crate test;
+extern crate rand;
+
+use test::{black_box, Bencher};
+
+use rand::{Rng, weak_rng};
+use rand::seq::*;
+
+#[bench]
+fn misc_shuffle_100(b: &mut Bencher) {
+ let mut rng = weak_rng();
+ let x : &mut [usize] = &mut [1; 100];
+ b.iter(|| {
+ rng.shuffle(x);
+ black_box(&x);
+ })
+}
+
+#[bench]
+fn misc_sample_iter_10_of_100(b: &mut Bencher) {
+ let mut rng = weak_rng();
+ let x : &[usize] = &[1; 100];
+ b.iter(|| {
+ black_box(sample_iter(&mut rng, x, 10).unwrap_or_else(|e| e));
+ })
+}
+
+#[bench]
+fn misc_sample_slice_10_of_100(b: &mut Bencher) {
+ let mut rng = weak_rng();
+ let x : &[usize] = &[1; 100];
+ b.iter(|| {
+ black_box(sample_slice(&mut rng, x, 10));
+ })
+}
+
+#[bench]
+fn misc_sample_slice_ref_10_of_100(b: &mut Bencher) {
+ let mut rng = weak_rng();
+ let x : &[usize] = &[1; 100];
+ b.iter(|| {
+ black_box(sample_slice_ref(&mut rng, x, 10));
+ })
+}
+
+macro_rules! sample_indices {
+ ($name:ident, $amount:expr, $length:expr) => {
+ #[bench]
+ fn $name(b: &mut Bencher) {
+ let mut rng = weak_rng();
+ b.iter(|| {
+ black_box(sample_indices(&mut rng, $length, $amount));
+ })
+ }
+ }
+}
+
+sample_indices!(misc_sample_indices_10_of_1k, 10, 1000);
+sample_indices!(misc_sample_indices_50_of_1k, 50, 1000);
+sample_indices!(misc_sample_indices_100_of_1k, 100, 1000);