Debian Codegraph @

benches/benchmarks.rs

raw symbols SCIP
1 //! Benchmarks.
2
3 use criterion::{
4 black_box, criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion,
5 PlotConfiguration,
6 };
7
8 use dashu_base::{CubicRoot, ExtendedGcd, Gcd, SquareRoot};
9 use rand::prelude::*;
10
11 const SEED: u64 = 1;
12
13 macro_rules! uop_case {
14 ($t:ty, $bits:literal, $method:ident, $rng:ident, $group:ident) => {
15 let bits = $bits;
16 let a: $t = $rng.gen_range(0..1 << $bits);
17 $group.bench_with_input(BenchmarkId::from_parameter(bits), &bits, |bencher, _| {
18 bencher.iter(|| black_box(a).$method())
19 });
20 };
21 }
22
23 macro_rules! binop_case {
24 ($t:ty, $bits:literal, $method:ident, $rng:ident, $group:ident) => {
25 let bits = $bits;
26 let a: $t = $rng.gen_range(0..1 << $bits);
27 let b: $t = $rng.gen_range(0..1 << $bits);
28 $group.bench_with_input(BenchmarkId::from_parameter(bits), &bits, |bencher, _| {
29 bencher.iter(|| black_box(a).$method(black_box(b)))
30 });
31 };
32 }
33
34 fn bench_gcd(criterion: &mut Criterion) {
35 let mut rng = StdRng::seed_from_u64(SEED);
36 let mut group = criterion.benchmark_group("gcd");
37 group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
38
39 binop_case!(u16, 10, gcd, rng, group);
40 binop_case!(u32, 20, gcd, rng, group);
41 binop_case!(u64, 40, gcd, rng, group);
42 binop_case!(u128, 80, gcd, rng, group);
43 binop_case!(u128, 120, gcd, rng, group);
44
45 group.finish();
46
47 let mut group = criterion.benchmark_group("gcd_ext");
48 group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
49
50 binop_case!(u16, 10, gcd_ext, rng, group);
51 binop_case!(u32, 20, gcd_ext, rng, group);
52 binop_case!(u64, 40, gcd_ext, rng, group);
53 binop_case!(u128, 80, gcd_ext, rng, group);
54 binop_case!(u128, 120, gcd_ext, rng, group);
55
56 group.finish();
57 }
58
59 fn bench_roots(criterion: &mut Criterion) {
60 let mut rng = StdRng::seed_from_u64(SEED);
61 let mut group = criterion.benchmark_group("sqrt");
62 group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
63
64 uop_case!(u16, 10, sqrt, rng, group);
65 uop_case!(u32, 20, sqrt, rng, group);
66 uop_case!(u64, 40, sqrt, rng, group);
67 uop_case!(u128, 80, sqrt, rng, group);
68 uop_case!(u128, 120, sqrt, rng, group);
69
70 group.finish();
71
72 let mut group = criterion.benchmark_group("cbrt");
73 group.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic));
74
75 uop_case!(u16, 10, cbrt, rng, group);
76 uop_case!(u32, 20, cbrt, rng, group);
77 uop_case!(u64, 40, cbrt, rng, group);
78 uop_case!(u128, 80, cbrt, rng, group);
79 uop_case!(u128, 120, cbrt, rng, group);
80
81 group.finish();
82 }
83
84 criterion_group!(benches, bench_gcd, bench_roots,);
85
86 criterion_main!(benches);