|
| 1 | +// Copyright 2021 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 | +//! Implement benchmarks for uniform distributions over integer types |
| 10 | +
|
| 11 | +use core::time::Duration; |
| 12 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 13 | +use rand::distributions::uniform::{SampleRange, Uniform}; |
| 14 | +use rand::prelude::*; |
| 15 | +use rand_chacha::ChaCha8Rng; |
| 16 | +use rand_pcg::{Pcg32, Pcg64}; |
| 17 | + |
| 18 | +const WARM_UP_TIME: Duration = Duration::from_millis(1000); |
| 19 | +const MEASUREMENT_TIME: Duration = Duration::from_secs(3); |
| 20 | +const SAMPLE_SIZE: usize = 100_000; |
| 21 | +const N_RESAMPLES: usize = 10_000; |
| 22 | + |
| 23 | +macro_rules! sample { |
| 24 | + ($R:ty, $T:ty, $U:ty, $g:expr) => { |
| 25 | + $g.bench_function(BenchmarkId::new(stringify!($R), "single"), |b| { |
| 26 | + let mut rng = <$R>::from_entropy(); |
| 27 | + let x = rng.gen::<$U>(); |
| 28 | + let bits = (<$T>::BITS / 2); |
| 29 | + let mask = (1 as $U).wrapping_neg() >> bits; |
| 30 | + let range = (x >> bits) * (x & mask); |
| 31 | + let low = <$T>::MIN; |
| 32 | + let high = low.wrapping_add(range as $T); |
| 33 | + |
| 34 | + b.iter(|| (low..=high).sample_single(&mut rng)); |
| 35 | + }); |
| 36 | + |
| 37 | + $g.bench_function(BenchmarkId::new(stringify!($R), "distr"), |b| { |
| 38 | + let mut rng = <$R>::from_entropy(); |
| 39 | + let x = rng.gen::<$U>(); |
| 40 | + let bits = (<$T>::BITS / 2); |
| 41 | + let mask = (1 as $U).wrapping_neg() >> bits; |
| 42 | + let range = (x >> bits) * (x & mask); |
| 43 | + let low = <$T>::MIN; |
| 44 | + let high = low.wrapping_add(range as $T); |
| 45 | + let dist = Uniform::<$T>::new_inclusive(<$T>::MIN, high).unwrap(); |
| 46 | + |
| 47 | + b.iter(|| dist.sample(&mut rng)); |
| 48 | + }); |
| 49 | + }; |
| 50 | + |
| 51 | + ($c:expr, $T:ty, $U:ty) => {{ |
| 52 | + let mut g = $c.benchmark_group(concat!("sample", stringify!($T))); |
| 53 | + g.sample_size(SAMPLE_SIZE); |
| 54 | + g.warm_up_time(WARM_UP_TIME); |
| 55 | + g.measurement_time(MEASUREMENT_TIME); |
| 56 | + g.nresamples(N_RESAMPLES); |
| 57 | + sample!(SmallRng, $T, $U, g); |
| 58 | + sample!(ChaCha8Rng, $T, $U, g); |
| 59 | + sample!(Pcg32, $T, $U, g); |
| 60 | + sample!(Pcg64, $T, $U, g); |
| 61 | + g.finish(); |
| 62 | + }}; |
| 63 | +} |
| 64 | + |
| 65 | +fn sample(c: &mut Criterion) { |
| 66 | + sample!(c, i8, u8); |
| 67 | + sample!(c, i16, u16); |
| 68 | + sample!(c, i32, u32); |
| 69 | + sample!(c, i64, u64); |
| 70 | + sample!(c, i128, u128); |
| 71 | +} |
| 72 | + |
| 73 | +criterion_group! { |
| 74 | + name = benches; |
| 75 | + config = Criterion::default(); |
| 76 | + targets = sample |
| 77 | +} |
| 78 | +criterion_main!(benches); |
0 commit comments