]> git.proxmox.com Git - rustc.git/blob - vendor/rand-0.7.3/benches/generators.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / vendor / rand-0.7.3 / benches / generators.rs
1 // Copyright 2018 Developers of the Rand project.
2 //
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.
8
9 #![feature(test)]
10 #![allow(non_snake_case)]
11
12 extern crate test;
13
14 const RAND_BENCH_N: u64 = 1000;
15 const BYTES_LEN: usize = 1024;
16
17 use std::mem::size_of;
18 use test::{black_box, Bencher};
19
20 use rand::prelude::*;
21 use rand::rngs::adapter::ReseedingRng;
22 use rand::rngs::{mock::StepRng, OsRng};
23 use rand_chacha::{ChaCha12Rng, ChaCha20Core, ChaCha20Rng, ChaCha8Rng};
24 use rand_hc::Hc128Rng;
25 use rand_pcg::{Pcg32, Pcg64, Pcg64Mcg};
26
27 macro_rules! gen_bytes {
28 ($fnn:ident, $gen:expr) => {
29 #[bench]
30 fn $fnn(b: &mut Bencher) {
31 let mut rng = $gen;
32 let mut buf = [0u8; BYTES_LEN];
33 b.iter(|| {
34 for _ in 0..RAND_BENCH_N {
35 rng.fill_bytes(&mut buf);
36 black_box(buf);
37 }
38 });
39 b.bytes = BYTES_LEN as u64 * RAND_BENCH_N;
40 }
41 };
42 }
43
44 gen_bytes!(gen_bytes_step, StepRng::new(0, 1));
45 gen_bytes!(gen_bytes_pcg32, Pcg32::from_entropy());
46 gen_bytes!(gen_bytes_pcg64, Pcg64::from_entropy());
47 gen_bytes!(gen_bytes_pcg64mcg, Pcg64Mcg::from_entropy());
48 gen_bytes!(gen_bytes_chacha8, ChaCha8Rng::from_entropy());
49 gen_bytes!(gen_bytes_chacha12, ChaCha12Rng::from_entropy());
50 gen_bytes!(gen_bytes_chacha20, ChaCha20Rng::from_entropy());
51 gen_bytes!(gen_bytes_hc128, Hc128Rng::from_entropy());
52 gen_bytes!(gen_bytes_std, StdRng::from_entropy());
53 #[cfg(feature = "small_rng")]
54 gen_bytes!(gen_bytes_small, SmallRng::from_entropy());
55 gen_bytes!(gen_bytes_os, OsRng);
56
57 macro_rules! gen_uint {
58 ($fnn:ident, $ty:ty, $gen:expr) => {
59 #[bench]
60 fn $fnn(b: &mut Bencher) {
61 let mut rng = $gen;
62 b.iter(|| {
63 let mut accum: $ty = 0;
64 for _ in 0..RAND_BENCH_N {
65 accum = accum.wrapping_add(rng.gen::<$ty>());
66 }
67 accum
68 });
69 b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
70 }
71 };
72 }
73
74 gen_uint!(gen_u32_step, u32, StepRng::new(0, 1));
75 gen_uint!(gen_u32_pcg32, u32, Pcg32::from_entropy());
76 gen_uint!(gen_u32_pcg64, u32, Pcg64::from_entropy());
77 gen_uint!(gen_u32_pcg64mcg, u32, Pcg64Mcg::from_entropy());
78 gen_uint!(gen_u32_chacha8, u32, ChaCha8Rng::from_entropy());
79 gen_uint!(gen_u32_chacha12, u32, ChaCha12Rng::from_entropy());
80 gen_uint!(gen_u32_chacha20, u32, ChaCha20Rng::from_entropy());
81 gen_uint!(gen_u32_hc128, u32, Hc128Rng::from_entropy());
82 gen_uint!(gen_u32_std, u32, StdRng::from_entropy());
83 #[cfg(feature = "small_rng")]
84 gen_uint!(gen_u32_small, u32, SmallRng::from_entropy());
85 gen_uint!(gen_u32_os, u32, OsRng);
86
87 gen_uint!(gen_u64_step, u64, StepRng::new(0, 1));
88 gen_uint!(gen_u64_pcg32, u64, Pcg32::from_entropy());
89 gen_uint!(gen_u64_pcg64, u64, Pcg64::from_entropy());
90 gen_uint!(gen_u64_pcg64mcg, u64, Pcg64Mcg::from_entropy());
91 gen_uint!(gen_u64_chacha8, u64, ChaCha8Rng::from_entropy());
92 gen_uint!(gen_u64_chacha12, u64, ChaCha12Rng::from_entropy());
93 gen_uint!(gen_u64_chacha20, u64, ChaCha20Rng::from_entropy());
94 gen_uint!(gen_u64_hc128, u64, Hc128Rng::from_entropy());
95 gen_uint!(gen_u64_std, u64, StdRng::from_entropy());
96 #[cfg(feature = "small_rng")]
97 gen_uint!(gen_u64_small, u64, SmallRng::from_entropy());
98 gen_uint!(gen_u64_os, u64, OsRng);
99
100 macro_rules! init_gen {
101 ($fnn:ident, $gen:ident) => {
102 #[bench]
103 fn $fnn(b: &mut Bencher) {
104 let mut rng = Pcg32::from_entropy();
105 b.iter(|| {
106 let r2 = $gen::from_rng(&mut rng).unwrap();
107 r2
108 });
109 }
110 };
111 }
112
113 init_gen!(init_pcg32, Pcg32);
114 init_gen!(init_pcg64, Pcg64);
115 init_gen!(init_pcg64mcg, Pcg64Mcg);
116 init_gen!(init_hc128, Hc128Rng);
117 init_gen!(init_chacha, ChaCha20Rng);
118
119 const RESEEDING_BYTES_LEN: usize = 1024 * 1024;
120 const RESEEDING_BENCH_N: u64 = 16;
121
122 macro_rules! reseeding_bytes {
123 ($fnn:ident, $thresh:expr) => {
124 #[bench]
125 fn $fnn(b: &mut Bencher) {
126 let mut rng = ReseedingRng::new(ChaCha20Core::from_entropy(), $thresh * 1024, OsRng);
127 let mut buf = [0u8; RESEEDING_BYTES_LEN];
128 b.iter(|| {
129 for _ in 0..RESEEDING_BENCH_N {
130 rng.fill_bytes(&mut buf);
131 black_box(&buf);
132 }
133 });
134 b.bytes = RESEEDING_BYTES_LEN as u64 * RESEEDING_BENCH_N;
135 }
136 };
137 }
138
139 reseeding_bytes!(reseeding_chacha20_4k, 4);
140 reseeding_bytes!(reseeding_chacha20_16k, 16);
141 reseeding_bytes!(reseeding_chacha20_32k, 32);
142 reseeding_bytes!(reseeding_chacha20_64k, 64);
143 reseeding_bytes!(reseeding_chacha20_256k, 256);
144 reseeding_bytes!(reseeding_chacha20_1M, 1024);
145
146
147 macro_rules! threadrng_uint {
148 ($fnn:ident, $ty:ty) => {
149 #[bench]
150 fn $fnn(b: &mut Bencher) {
151 let mut rng = thread_rng();
152 b.iter(|| {
153 let mut accum: $ty = 0;
154 for _ in 0..RAND_BENCH_N {
155 accum = accum.wrapping_add(rng.gen::<$ty>());
156 }
157 accum
158 });
159 b.bytes = size_of::<$ty>() as u64 * RAND_BENCH_N;
160 }
161 };
162 }
163
164 threadrng_uint!(thread_rng_u32, u32);
165 threadrng_uint!(thread_rng_u64, u64);