| 1 |
|
| 2 |
|
| 3 |
use criterion::{ |
| 4 |
black_box, criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion, |
| 5 |
}; |
| 6 |
use rust_decimal::Decimal; |
| 7 |
|
| 8 |
const DECIMAL_2_01: Decimal = Decimal::from_parts(201, 0, 0, false, 2); |
| 9 |
const DECIMAL_13_7: Decimal = Decimal::from_parts(137, 0, 0, false, 1); |
| 10 |
const F64_2_01: f64 = 2.01; |
| 11 |
const F64_13_7: f64 = 13.7; |
| 12 |
|
| 13 |
macro_rules! add_benchmark_group { |
| 14 |
($criterion:expr, $f:ident, $op:tt) => { |
| 15 |
fn $f<M, const N: usize>(group: &mut BenchmarkGroup<'_, M>) |
| 16 |
where |
| 17 |
M: Measurement, |
| 18 |
{ |
| 19 |
group.bench_with_input(BenchmarkId::new("f64 (diff)", N), &N, |ben, _| { |
| 20 |
ben.iter(|| black_box(F64_2_01 $op F64_13_7)) |
| 21 |
}); |
| 22 |
|
| 23 |
group.bench_with_input(BenchmarkId::new("f64 (equal)", N), &N, |ben, _| { |
| 24 |
ben.iter(|| black_box(F64_2_01 $op F64_2_01)) |
| 25 |
}); |
| 26 |
|
| 27 |
group.bench_with_input(BenchmarkId::new("rust-decimal (diff)", N), &N, |ben, _| { |
| 28 |
ben.iter(|| black_box(DECIMAL_2_01 $op DECIMAL_13_7)) |
| 29 |
}); |
| 30 |
|
| 31 |
group.bench_with_input(BenchmarkId::new("rust-decimal (equal)", N), &N, |ben, _| { |
| 32 |
ben.iter(|| black_box(DECIMAL_2_01 $op DECIMAL_2_01)) |
| 33 |
}); |
| 34 |
} |
| 35 |
|
| 36 |
let mut group = $criterion.benchmark_group(stringify!($f)); |
| 37 |
$f::<_, 100>(&mut group); |
| 38 |
group.finish(); |
| 39 |
}; |
| 40 |
} |
| 41 |
|
| 42 |
fn criterion_benchmark(c: &mut Criterion) { |
| 43 |
add_benchmark_group!(c, addition, +); |
| 44 |
add_benchmark_group!(c, division, /); |
| 45 |
add_benchmark_group!(c, multiplication, *); |
| 46 |
add_benchmark_group!(c, subtraction, -); |
| 47 |
} |
| 48 |
|
| 49 |
criterion_group!(benches, criterion_benchmark); |
| 50 |
criterion_main!(benches); |