Skip to content

Commit 657f86e

Browse files
committed
Add benchmarks for gen_bool and SmallRng
1 parent 00b7616 commit 657f86e

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

benches/generators.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const BYTES_LEN: usize = 1024;
99
use std::mem::size_of;
1010
use test::{black_box, Bencher};
1111

12-
use rand::{RngCore, Rng, SeedableRng, NewRng, StdRng, OsRng, JitterRng, EntropyRng};
12+
use rand::{RngCore, Rng, SeedableRng, NewRng};
13+
use rand::{StdRng, SmallRng, OsRng, JitterRng, EntropyRng};
1314
use rand::{XorShiftRng, Hc128Rng, IsaacRng, Isaac64Rng, ChaChaRng};
1415
use rand::reseeding::ReseedingRng;
1516
use rand::prng::hc128::Hc128Core;
@@ -37,6 +38,7 @@ gen_bytes!(gen_bytes_hc128, Hc128Rng::new());
3738
gen_bytes!(gen_bytes_isaac, IsaacRng::new());
3839
gen_bytes!(gen_bytes_isaac64, Isaac64Rng::new());
3940
gen_bytes!(gen_bytes_std, StdRng::new());
41+
gen_bytes!(gen_bytes_small, SmallRng::new());
4042
gen_bytes!(gen_bytes_os, OsRng::new().unwrap());
4143

4244
macro_rules! gen_uint {
@@ -59,13 +61,15 @@ gen_uint!(gen_u32_hc128, u32, Hc128Rng::new());
5961
gen_uint!(gen_u32_isaac, u32, IsaacRng::new());
6062
gen_uint!(gen_u32_isaac64, u32, Isaac64Rng::new());
6163
gen_uint!(gen_u32_std, u32, StdRng::new());
64+
gen_uint!(gen_u32_small, u32, SmallRng::new());
6265
gen_uint!(gen_u32_os, u32, OsRng::new().unwrap());
6366

6467
gen_uint!(gen_u64_xorshift, u64, XorShiftRng::new());
6568
gen_uint!(gen_u64_hc128, u64, Hc128Rng::new());
6669
gen_uint!(gen_u64_isaac, u64, IsaacRng::new());
6770
gen_uint!(gen_u64_isaac64, u64, Isaac64Rng::new());
6871
gen_uint!(gen_u64_std, u64, StdRng::new());
72+
gen_uint!(gen_u64_small, u64, SmallRng::new());
6973
gen_uint!(gen_u64_os, u64, OsRng::new().unwrap());
7074

7175
// Do not test JitterRng like the others by running it RAND_BENCH_N times per,

benches/misc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,35 @@
33
extern crate test;
44
extern crate rand;
55

6+
const RAND_BENCH_N: u64 = 1000;
7+
68
use test::{black_box, Bencher};
79

810
use rand::{SeedableRng, SmallRng, Rng, thread_rng};
911
use rand::seq::*;
1012

13+
#[bench]
14+
fn misc_gen_bool(b: &mut Bencher) {
15+
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
16+
b.iter(|| {
17+
for _ in 0..::RAND_BENCH_N {
18+
black_box(rng.gen_bool(0.18));
19+
}
20+
})
21+
}
22+
23+
#[bench]
24+
fn misc_gen_bool_var(b: &mut Bencher) {
25+
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
26+
b.iter(|| {
27+
for _ in 0..::RAND_BENCH_N {
28+
let mut p = 0.18;
29+
black_box(rng.gen_bool(p));
30+
black_box(p);
31+
}
32+
})
33+
}
34+
1135
#[bench]
1236
fn misc_shuffle_100(b: &mut Bencher) {
1337
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ impl<R: SeedableRng> NewRng for R {
816816
}
817817
}
818818

819-
/// The standard RNG. The PRNG algorithm in `StdRng` is choosen to be efficient
819+
/// The standard RNG. The PRNG algorithm in `StdRng` is chosen to be efficient
820820
/// on the current platform, to be statistically strong and unpredictable
821821
/// (meaning a cryptographically secure PRNG).
822822
///
@@ -865,7 +865,7 @@ impl SeedableRng for StdRng {
865865
}
866866

867867
/// An RNG recommended when small state, cheap initialization and good
868-
/// performance are required. The PRNG algorithm in `SmallRng` is choosen to be
868+
/// performance are required. The PRNG algorithm in `SmallRng` is chosen to be
869869
/// efficient on the current platform, **without consideration for cryptography
870870
/// or security**. The size of its state is much smaller than for `StdRng`.
871871
///
@@ -908,10 +908,12 @@ impl SeedableRng for StdRng {
908908
pub struct SmallRng(XorShiftRng);
909909

910910
impl RngCore for SmallRng {
911+
#[inline(always)]
911912
fn next_u32(&mut self) -> u32 {
912913
self.0.next_u32()
913914
}
914915

916+
#[inline(always)]
915917
fn next_u64(&mut self) -> u64 {
916918
self.0.next_u64()
917919
}

0 commit comments

Comments
 (0)