|
| 1 | +// Copyright 2023 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 FP types |
| 10 | +//! |
| 11 | +//! Sampling methods compared: |
| 12 | +//! |
| 13 | +//! - sample: current method: (x12 - 1.0) * (b - a) + a |
| 14 | +
|
| 15 | +use core::time::Duration; |
| 16 | +use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; |
| 17 | +use rand::distributions::uniform::{SampleUniform, Uniform, UniformSampler}; |
| 18 | +use rand::prelude::*; |
| 19 | +use rand_chacha::ChaCha8Rng; |
| 20 | +use rand_pcg::{Pcg32, Pcg64}; |
| 21 | + |
| 22 | +const WARM_UP_TIME: Duration = Duration::from_millis(1000); |
| 23 | +const MEASUREMENT_TIME: Duration = Duration::from_secs(3); |
| 24 | +const SAMPLE_SIZE: usize = 100_000; |
| 25 | +const N_RESAMPLES: usize = 10_000; |
| 26 | + |
| 27 | +macro_rules! single_random { |
| 28 | + ($R:ty, $T:ty, $g:expr) => { |
| 29 | + $g.bench_function(BenchmarkId::new(stringify!($T), stringify!($R)), |b| { |
| 30 | + let mut rng = <$R>::from_entropy(); |
| 31 | + let (mut low, mut high); |
| 32 | + loop { |
| 33 | + low = <$T>::from_bits(rng.gen()); |
| 34 | + high = <$T>::from_bits(rng.gen()); |
| 35 | + if (low < high) && (high - low).is_normal() { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + b.iter(|| <$T as SampleUniform>::Sampler::sample_single_inclusive(low, high, &mut rng)); |
| 41 | + }); |
| 42 | + }; |
| 43 | + |
| 44 | + ($c:expr, $T:ty) => {{ |
| 45 | + let mut g = $c.benchmark_group("uniform_single"); |
| 46 | + g.sample_size(SAMPLE_SIZE); |
| 47 | + g.warm_up_time(WARM_UP_TIME); |
| 48 | + g.measurement_time(MEASUREMENT_TIME); |
| 49 | + g.nresamples(N_RESAMPLES); |
| 50 | + single_random!(SmallRng, $T, g); |
| 51 | + single_random!(ChaCha8Rng, $T, g); |
| 52 | + single_random!(Pcg32, $T, g); |
| 53 | + single_random!(Pcg64, $T, g); |
| 54 | + g.finish(); |
| 55 | + }}; |
| 56 | +} |
| 57 | + |
| 58 | +fn single_random(c: &mut Criterion) { |
| 59 | + single_random!(c, f32); |
| 60 | + single_random!(c, f64); |
| 61 | +} |
| 62 | + |
| 63 | +macro_rules! distr_random { |
| 64 | + ($R:ty, $T:ty, $g:expr) => { |
| 65 | + $g.bench_function(BenchmarkId::new(stringify!($T), stringify!($R)), |b| { |
| 66 | + let mut rng = <$R>::from_entropy(); |
| 67 | + let dist = loop { |
| 68 | + let low = <$T>::from_bits(rng.gen()); |
| 69 | + let high = <$T>::from_bits(rng.gen()); |
| 70 | + if let Ok(dist) = Uniform::<$T>::new_inclusive(low, high) { |
| 71 | + break dist; |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + b.iter(|| dist.sample(&mut rng)); |
| 76 | + }); |
| 77 | + }; |
| 78 | + |
| 79 | + ($c:expr, $T:ty) => {{ |
| 80 | + let mut g = $c.benchmark_group("uniform_distribution"); |
| 81 | + g.sample_size(SAMPLE_SIZE); |
| 82 | + g.warm_up_time(WARM_UP_TIME); |
| 83 | + g.measurement_time(MEASUREMENT_TIME); |
| 84 | + g.nresamples(N_RESAMPLES); |
| 85 | + distr_random!(SmallRng, $T, g); |
| 86 | + distr_random!(ChaCha8Rng, $T, g); |
| 87 | + distr_random!(Pcg32, $T, g); |
| 88 | + distr_random!(Pcg64, $T, g); |
| 89 | + g.finish(); |
| 90 | + }}; |
| 91 | +} |
| 92 | + |
| 93 | +fn distr_random(c: &mut Criterion) { |
| 94 | + distr_random!(c, f32); |
| 95 | + distr_random!(c, f64); |
| 96 | +} |
| 97 | + |
| 98 | +criterion_group! { |
| 99 | + name = benches; |
| 100 | + config = Criterion::default(); |
| 101 | + targets = single_random, distr_random |
| 102 | +} |
| 103 | +criterion_main!(benches); |
0 commit comments