diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/rust_decimal/benches/comparison.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/rust_decimal/benches/comparison.rs')
-rw-r--r-- | third_party/rust/rust_decimal/benches/comparison.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/third_party/rust/rust_decimal/benches/comparison.rs b/third_party/rust/rust_decimal/benches/comparison.rs new file mode 100644 index 0000000000..d6bf29adba --- /dev/null +++ b/third_party/rust/rust_decimal/benches/comparison.rs @@ -0,0 +1,50 @@ +//! See how `rust-decimal` performs compared to native floating numbers. + +use criterion::{ + black_box, criterion_group, criterion_main, measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion, +}; +use rust_decimal::Decimal; + +const DECIMAL_2_01: Decimal = Decimal::from_parts(201, 0, 0, false, 2); +const DECIMAL_13_7: Decimal = Decimal::from_parts(137, 0, 0, false, 1); +const F64_2_01: f64 = 2.01; +const F64_13_7: f64 = 13.7; + +macro_rules! add_benchmark_group { + ($criterion:expr, $f:ident, $op:tt) => { + fn $f<M, const N: usize>(group: &mut BenchmarkGroup<'_, M>) + where + M: Measurement, + { + group.bench_with_input(BenchmarkId::new("f64 (diff)", N), &N, |ben, _| { + ben.iter(|| black_box(F64_2_01 $op F64_13_7)) + }); + + group.bench_with_input(BenchmarkId::new("f64 (equal)", N), &N, |ben, _| { + ben.iter(|| black_box(F64_2_01 $op F64_2_01)) + }); + + group.bench_with_input(BenchmarkId::new("rust-decimal (diff)", N), &N, |ben, _| { + ben.iter(|| black_box(DECIMAL_2_01 $op DECIMAL_13_7)) + }); + + group.bench_with_input(BenchmarkId::new("rust-decimal (equal)", N), &N, |ben, _| { + ben.iter(|| black_box(DECIMAL_2_01 $op DECIMAL_2_01)) + }); + } + + let mut group = $criterion.benchmark_group(stringify!($f)); + $f::<_, 100>(&mut group); + group.finish(); + }; +} + +fn criterion_benchmark(c: &mut Criterion) { + add_benchmark_group!(c, addition, +); + add_benchmark_group!(c, division, /); + add_benchmark_group!(c, multiplication, *); + add_benchmark_group!(c, subtraction, -); +} + +criterion_group!(benches, criterion_benchmark); +criterion_main!(benches); |