Debian Codegraph @

debian/missing-sources/time/benchmarks/rand09.rs

raw
1 use criterion::Bencher;
2 use rand09::Rng;
3 use time::{Date, Duration, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset, Weekday};
4
5 macro_rules! bench_rand {
6 ($($name:ident : $type:ty),* $(,)?) => {
7 setup_benchmark! {
8 "Random",
9 $(fn $name(ben: &mut Bencher<'_>) {
10 iter_batched_ref!(
11 ben,
12 || StepRng::new(0, 1),
13 [|rng| rng.random::<$type>()]
14 );
15 })*
16 }
17 }
18 }
19
20 bench_rand![
21 time: Time,
22 date: Date,
23 utc_offset: UtcOffset,
24 primitive_date_time: PrimitiveDateTime,
25 offset_date_time: OffsetDateTime,
26 duration: Duration,
27 weekday: Weekday,
28 month: Month,
29 ];
30
31 // copy of `StepRng` from rand 0.8 to avoid deprecation warnings
32 #[derive(Debug, Clone)]
33 struct StepRng {
34 v: u64,
35 a: u64,
36 }
37
38 impl StepRng {
39 const fn new(initial: u64, increment: u64) -> Self {
40 Self {
41 v: initial,
42 a: increment,
43 }
44 }
45 }
46
47 impl rand09::RngCore for StepRng {
48 fn next_u32(&mut self) -> u32 {
49 self.next_u64() as u32
50 }
51
52 fn next_u64(&mut self) -> u64 {
53 let res = self.v;
54 self.v = self.v.wrapping_add(self.a);
55 res
56 }
57
58 fn fill_bytes(&mut self, dst: &mut [u8]) {
59 rand09::rand_core::impls::fill_bytes_via_next(self, dst)
60 }
61 }