-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathto_f64.rs
49 lines (44 loc) · 1.5 KB
/
to_f64.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use const_decimal::{Decimal, ScaledInteger};
use criterion::measurement::WallTime;
use criterion::{black_box, BatchSize, BenchmarkGroup};
use num_traits::PrimInt;
use prop::strategy::ValueTree;
use prop::test_runner::TestRunner;
use proptest::prelude::*;
pub fn bench_all<const D: u8, I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: ScaledInteger<D> + Arbitrary,
{
bench_primitive_to_f64::<I>(group);
bench_decimal_to_f64::<D, I>(group);
}
fn bench_primitive_to_f64<I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: PrimInt + Arbitrary,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
let input = (I::arbitrary(), I::arbitrary());
group.bench_function("primitive/to_f64", |bencher| {
bencher.iter_batched(
|| input.new_tree(&mut runner).unwrap().current(),
|(a, b)| black_box(black_box(a) + black_box(b)),
BatchSize::SmallInput,
)
});
}
fn bench_decimal_to_f64<const D: u8, I>(group: &mut BenchmarkGroup<'_, WallTime>)
where
I: ScaledInteger<D> + Arbitrary,
{
// Use proptest to generate arbitrary input values.
let mut runner = TestRunner::deterministic();
let input = I::arbitrary().prop_map(|a| Decimal::<_, D>(a));
group.bench_function("decimal/to_f64", |bencher| {
bencher.iter_batched(
|| input.new_tree(&mut runner).unwrap().current(),
|a| black_box(a.to_f64()),
BatchSize::SmallInput,
)
});
}