]> git.proxmox.com Git - cargo.git/blob - vendor/rand-0.5.6/benches/distributions.rs
New upstream version 0.33.0
[cargo.git] / vendor / rand-0.5.6 / benches / distributions.rs
1 #![feature(test)]
2 #![cfg_attr(all(feature="i128_support", feature="nightly"), allow(stable_features))] // stable since 2018-03-27
3 #![cfg_attr(all(feature="i128_support", feature="nightly"), feature(i128_type, i128))]
4
5 extern crate test;
6 extern crate rand;
7
8 const RAND_BENCH_N: u64 = 1000;
9
10 use std::mem::size_of;
11 use test::Bencher;
12
13 use rand::{Rng, FromEntropy, XorShiftRng};
14 use rand::distributions::*;
15
16 macro_rules! distr_int {
17 ($fnn:ident, $ty:ty, $distr:expr) => {
18 #[bench]
19 fn $fnn(b: &mut Bencher) {
20 let mut rng = XorShiftRng::from_entropy();
21 let distr = $distr;
22
23 b.iter(|| {
24 let mut accum = 0 as $ty;
25 for _ in 0..::RAND_BENCH_N {
26 let x: $ty = distr.sample(&mut rng);
27 accum = accum.wrapping_add(x);
28 }
29 accum
30 });
31 b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
32 }
33 }
34 }
35
36 macro_rules! distr_float {
37 ($fnn:ident, $ty:ty, $distr:expr) => {
38 #[bench]
39 fn $fnn(b: &mut Bencher) {
40 let mut rng = XorShiftRng::from_entropy();
41 let distr = $distr;
42
43 b.iter(|| {
44 let mut accum = 0.0;
45 for _ in 0..::RAND_BENCH_N {
46 let x: $ty = distr.sample(&mut rng);
47 accum += x;
48 }
49 accum
50 });
51 b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
52 }
53 }
54 }
55
56 macro_rules! distr {
57 ($fnn:ident, $ty:ty, $distr:expr) => {
58 #[bench]
59 fn $fnn(b: &mut Bencher) {
60 let mut rng = XorShiftRng::from_entropy();
61 let distr = $distr;
62
63 b.iter(|| {
64 let mut accum = 0u32;
65 for _ in 0..::RAND_BENCH_N {
66 let x: $ty = distr.sample(&mut rng);
67 accum = accum.wrapping_add(x as u32);
68 }
69 accum
70 });
71 b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
72 }
73 }
74 }
75
76 // uniform
77 distr_int!(distr_uniform_i8, i8, Uniform::new(20i8, 100));
78 distr_int!(distr_uniform_i16, i16, Uniform::new(-500i16, 2000));
79 distr_int!(distr_uniform_i32, i32, Uniform::new(-200_000_000i32, 800_000_000));
80 distr_int!(distr_uniform_i64, i64, Uniform::new(3i64, 123_456_789_123));
81 #[cfg(feature = "i128_support")]
82 distr_int!(distr_uniform_i128, i128, Uniform::new(-123_456_789_123i128, 123_456_789_123_456_789));
83
84 distr_float!(distr_uniform_f32, f32, Uniform::new(2.26f32, 2.319));
85 distr_float!(distr_uniform_f64, f64, Uniform::new(2.26f64, 2.319));
86
87 // standard
88 distr_int!(distr_standard_i8, i8, Standard);
89 distr_int!(distr_standard_i16, i16, Standard);
90 distr_int!(distr_standard_i32, i32, Standard);
91 distr_int!(distr_standard_i64, i64, Standard);
92 #[cfg(feature = "i128_support")]
93 distr_int!(distr_standard_i128, i128, Standard);
94
95 distr!(distr_standard_bool, bool, Standard);
96 distr!(distr_standard_alphanumeric, char, Alphanumeric);
97 distr!(distr_standard_codepoint, char, Standard);
98
99 distr_float!(distr_standard_f32, f32, Standard);
100 distr_float!(distr_standard_f64, f64, Standard);
101 distr_float!(distr_open01_f32, f32, Open01);
102 distr_float!(distr_open01_f64, f64, Open01);
103 distr_float!(distr_openclosed01_f32, f32, OpenClosed01);
104 distr_float!(distr_openclosed01_f64, f64, OpenClosed01);
105
106 // distributions
107 distr_float!(distr_exp, f64, Exp::new(1.23 * 4.56));
108 distr_float!(distr_normal, f64, Normal::new(-1.23, 4.56));
109 distr_float!(distr_log_normal, f64, LogNormal::new(-1.23, 4.56));
110 distr_float!(distr_gamma_large_shape, f64, Gamma::new(10., 1.0));
111 distr_float!(distr_gamma_small_shape, f64, Gamma::new(0.1, 1.0));
112 distr_float!(distr_cauchy, f64, Cauchy::new(4.2, 6.9));
113 distr_int!(distr_binomial, u64, Binomial::new(20, 0.7));
114 distr_int!(distr_poisson, u64, Poisson::new(4.0));
115 distr!(distr_bernoulli, bool, Bernoulli::new(0.18));
116
117
118 // construct and sample from a range
119 macro_rules! gen_range_int {
120 ($fnn:ident, $ty:ident, $low:expr, $high:expr) => {
121 #[bench]
122 fn $fnn(b: &mut Bencher) {
123 let mut rng = XorShiftRng::from_entropy();
124
125 b.iter(|| {
126 let mut high = $high;
127 let mut accum: $ty = 0;
128 for _ in 0..::RAND_BENCH_N {
129 accum = accum.wrapping_add(rng.gen_range($low, high));
130 // force recalculation of range each time
131 high = high.wrapping_add(1) & std::$ty::MAX;
132 }
133 accum
134 });
135 b.bytes = size_of::<$ty>() as u64 * ::RAND_BENCH_N;
136 }
137 }
138 }
139
140 gen_range_int!(gen_range_i8, i8, -20i8, 100);
141 gen_range_int!(gen_range_i16, i16, -500i16, 2000);
142 gen_range_int!(gen_range_i32, i32, -200_000_000i32, 800_000_000);
143 gen_range_int!(gen_range_i64, i64, 3i64, 123_456_789_123);
144 #[cfg(feature = "i128_support")]
145 gen_range_int!(gen_range_i128, i128, -12345678901234i128, 123_456_789_123_456_789);
146
147 #[bench]
148 fn dist_iter(b: &mut Bencher) {
149 let mut rng = XorShiftRng::from_entropy();
150 let distr = Normal::new(-2.71828, 3.14159);
151 let mut iter = distr.sample_iter(&mut rng);
152
153 b.iter(|| {
154 let mut accum = 0.0;
155 for _ in 0..::RAND_BENCH_N {
156 accum += iter.next().unwrap();
157 }
158 accum
159 });
160 b.bytes = size_of::<f64>() as u64 * ::RAND_BENCH_N;
161 }