1 // Copyright 2018 Developers of the Rand project.
3 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4 // https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5 // <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6 // option. This file may not be copied, modified, or distributed
7 // except according to those terms.
13 const RAND_BENCH_N
: u64 = 1000;
17 use rand
::distributions
::{Bernoulli, Distribution, Standard}
;
19 use rand_pcg
::{Pcg32, Pcg64Mcg}
;
22 fn misc_gen_bool_const(b
: &mut Bencher
) {
23 let mut rng
= Pcg32
::from_rng(&mut thread_rng()).unwrap();
26 for _
in 0..crate::RAND_BENCH_N
{
27 accum ^
= rng
.gen_bool(0.18);
34 fn misc_gen_bool_var(b
: &mut Bencher
) {
35 let mut rng
= Pcg32
::from_rng(&mut thread_rng()).unwrap();
39 for _
in 0..crate::RAND_BENCH_N
{
40 accum ^
= rng
.gen_bool(p
);
48 fn misc_gen_ratio_const(b
: &mut Bencher
) {
49 let mut rng
= Pcg32
::from_rng(&mut thread_rng()).unwrap();
52 for _
in 0..crate::RAND_BENCH_N
{
53 accum ^
= rng
.gen_ratio(2, 3);
60 fn misc_gen_ratio_var(b
: &mut Bencher
) {
61 let mut rng
= Pcg32
::from_rng(&mut thread_rng()).unwrap();
64 for i
in 2..(crate::RAND_BENCH_N
as u32 + 2) {
65 accum ^
= rng
.gen_ratio(i
, i
+ 1);
72 fn misc_bernoulli_const(b
: &mut Bencher
) {
73 let mut rng
= Pcg32
::from_rng(&mut thread_rng()).unwrap();
75 let d
= rand
::distributions
::Bernoulli
::new(0.18).unwrap();
77 for _
in 0..crate::RAND_BENCH_N
{
78 accum ^
= rng
.sample(d
);
85 fn misc_bernoulli_var(b
: &mut Bencher
) {
86 let mut rng
= Pcg32
::from_rng(&mut thread_rng()).unwrap();
90 for _
in 0..crate::RAND_BENCH_N
{
91 let d
= Bernoulli
::new(p
).unwrap();
92 accum ^
= rng
.sample(d
);
100 fn gen_1k_iter_repeat(b
: &mut Bencher
) {
102 let mut rng
= Pcg64Mcg
::from_rng(&mut thread_rng()).unwrap();
104 let v
: Vec
<u64> = iter
::repeat(()).map(|()| rng
.gen()).take(128).collect();
111 fn gen_1k_sample_iter(b
: &mut Bencher
) {
112 let mut rng
= Pcg64Mcg
::from_rng(&mut thread_rng()).unwrap();
114 let v
: Vec
<u64> = Standard
.sample_iter(&mut rng
).take(128).collect();
121 fn gen_1k_gen_array(b
: &mut Bencher
) {
122 let mut rng
= Pcg64Mcg
::from_rng(&mut thread_rng()).unwrap();
124 // max supported array length is 32!
125 let v
: [[u64; 32]; 4] = rng
.gen();
132 fn gen_1k_fill(b
: &mut Bencher
) {
133 let mut rng
= Pcg64Mcg
::from_rng(&mut thread_rng()).unwrap();
134 let mut buf
= [0u64; 128];
136 rng
.fill(&mut buf
[..]);