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 | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/rust_decimal/benches')
-rw-r--r-- | third_party/rust/rust_decimal/benches/comparison.rs | 50 | ||||
-rw-r--r-- | third_party/rust/rust_decimal/benches/lib_benches.rs | 359 |
2 files changed, 409 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); diff --git a/third_party/rust/rust_decimal/benches/lib_benches.rs b/third_party/rust/rust_decimal/benches/lib_benches.rs new file mode 100644 index 0000000000..5757c91c84 --- /dev/null +++ b/third_party/rust/rust_decimal/benches/lib_benches.rs @@ -0,0 +1,359 @@ +#![feature(test)] + +extern crate test; + +use bincode::Options as _; +use core::str::FromStr; +use rust_decimal::Decimal; + +macro_rules! bench_decimal_op { + ($name:ident, $op:tt, $x:expr, $y:expr) => { + #[bench] + fn $name(b: &mut ::test::Bencher) { + let x = Decimal::from_str($x).unwrap(); + let y = Decimal::from_str($y).unwrap(); + b.iter(|| { + let result = x $op y; + ::test::black_box(result); + }); + } + } +} + +macro_rules! bench_fold_op { + ($name:ident, $op:tt, $init:expr, $count:expr) => { + #[bench] + fn $name(b: &mut ::test::Bencher) { + fn fold(values: &[Decimal]) -> Decimal { + let mut acc: Decimal = $init.into(); + for value in values { + acc = acc $op value; + } + acc + } + + let values: Vec<Decimal> = test::black_box((1..$count).map(|i| i.into()).collect()); + b.iter(|| { + let result = fold(&values); + ::test::black_box(result); + }); + } + } +} + +/* Add */ +bench_decimal_op!(add_self, +, "2.01", "2.01"); +bench_decimal_op!(add_simple, +, "2", "1"); +bench_decimal_op!(add_one, +, "2.01", "1"); +bench_decimal_op!(add_two, +, "2.01", "2"); +bench_decimal_op!(add_one_hundred, +, "2.01", "100"); +bench_decimal_op!(add_point_zero_one, +, "2.01", "0.01"); +bench_decimal_op!(add_negative_point_five, +, "2.01", "-0.5"); +bench_decimal_op!(add_pi, +, "2.01", "3.1415926535897932384626433832"); +bench_decimal_op!(add_negative_pi, +, "2.01", "-3.1415926535897932384626433832"); + +bench_fold_op!(add_10k, +, 0, 10_000); + +/* Sub */ +bench_decimal_op!(sub_self, -, "2.01", "2.01"); +bench_decimal_op!(sub_simple, -, "2", "1"); +bench_decimal_op!(sub_one, -, "2.01", "1"); +bench_decimal_op!(sub_two, -, "2.01", "2"); +bench_decimal_op!(sub_one_hundred, -, "2.01", "100"); +bench_decimal_op!(sub_point_zero_one, -, "2.01", "0.01"); +bench_decimal_op!(sub_negative_point_five, -, "2.01", "-0.5"); +bench_decimal_op!(sub_pi, -, "2.01", "3.1415926535897932384626433832"); +bench_decimal_op!(sub_negative_pi, -, "2.01", "-3.1415926535897932384626433832"); + +bench_fold_op!(sub_10k, -, 5_000_000, 10_000); + +/* Mul */ +bench_decimal_op!(mul_one, *, "2.01", "1"); +bench_decimal_op!(mul_two, *, "2.01", "2"); +bench_decimal_op!(mul_one_hundred, *, "2.01", "100"); +bench_decimal_op!(mul_point_zero_one, *, "2.01", "0.01"); +bench_decimal_op!(mul_negative_point_five, *, "2.01", "-0.5"); +bench_decimal_op!(mul_pi, *, "2.01", "3.1415926535897932384626433832"); +bench_decimal_op!(mul_negative_pi, *, "2.01", "-3.1415926535897932384626433832"); + +bench_fold_op!(mul_25, *, Decimal::from_str("1.1").unwrap(), 25); + +/* Div */ +bench_decimal_op!(div_one, /, "2.01", "1"); +bench_decimal_op!(div_two, /, "2.01", "2"); +bench_decimal_op!(div_one_hundred, /, "2.01", "100"); +bench_decimal_op!(div_point_zero_one, /, "2.01", "0.01"); +bench_decimal_op!(div_negative_point_five, /, "2.01", "-0.5"); +bench_decimal_op!(div_pi, /, "2.01", "3.1415926535897932384626433832"); +bench_decimal_op!(div_negative_pi, /, "2.01", "-3.1415926535897932384626433832"); +bench_decimal_op!(div_no_underflow, /, "1.02343545345", "0.35454343453"); +bench_fold_op!(div_10k, /, Decimal::MAX, 10_000); +bench_fold_op!(rem_10k, %, Decimal::MAX, 10_000); + +/* Iteration */ +struct DecimalIterator { + count: usize, +} + +impl DecimalIterator { + fn new() -> DecimalIterator { + DecimalIterator { count: 0 } + } +} + +impl Iterator for DecimalIterator { + type Item = Decimal; + + fn next(&mut self) -> Option<Decimal> { + self.count += 1; + if self.count < 6 { + Some(Decimal::new(314, 2)) + } else { + None + } + } +} + +#[bench] +fn iterator_individual(b: &mut ::test::Bencher) { + b.iter(|| { + let mut result = Decimal::new(0, 0); + let iterator = DecimalIterator::new(); + for i in iterator { + result += i; + } + ::test::black_box(result); + }); +} + +#[bench] +fn iterator_product(b: &mut ::test::Bencher) { + b.iter(|| { + let result: Decimal = DecimalIterator::new().product(); + ::test::black_box(result); + }); +} + +#[bench] +fn iterator_sum(b: &mut ::test::Bencher) { + b.iter(|| { + let result: Decimal = DecimalIterator::new().sum(); + ::test::black_box(result); + }); +} + +const SAMPLE_STRS: &[&str] = &[ + "3950.123456", + "3950", + "0.1", + "0.01", + "0.001", + "0.0001", + "0.00001", + "0.000001", + "1", + "-100", + "-123.456", + "119996.25", + "1000000", + "9999999.99999", + "12340.56789", +]; + +#[bench] +fn serialize_bincode(b: &mut test::Bencher) { + let decimals: Vec<Decimal> = SAMPLE_STRS.iter().map(|s| Decimal::from_str(s).unwrap()).collect(); + + b.iter(|| { + for d in &decimals { + let bytes = bincode::options().serialize(d).unwrap(); + test::black_box(bytes); + } + }) +} + +#[cfg(feature = "serde-str")] +#[bench] +fn deserialize_bincode(b: &mut test::Bencher) { + let payloads: Vec<Vec<u8>> = SAMPLE_STRS + .iter() + .map(|s| bincode::options().serialize(&Decimal::from_str(s).unwrap()).unwrap()) + .collect(); + + b.iter(|| { + for payload in &payloads { + let decimal: Decimal = bincode::options().deserialize(payload).unwrap(); + test::black_box(decimal); + } + }) +} + +#[bench] +fn decimal_from_str(b: &mut test::Bencher) { + b.iter(|| { + for s in SAMPLE_STRS { + let result = Decimal::from_str(s).unwrap(); + test::black_box(result); + } + }) +} + +#[bench] +fn decimal_to_string(b: &mut test::Bencher) { + let decimals: Vec<Decimal> = SAMPLE_STRS.iter().map(|s| Decimal::from_str(s).unwrap()).collect(); + + b.iter(|| { + for s in decimals.iter() { + let string = s.to_string(); + test::black_box(string); + } + }) +} + +#[cfg(feature = "postgres")] +#[bench] +fn to_from_sql(b: &mut ::test::Bencher) { + use bytes::BytesMut; + use postgres::types::{FromSql, Kind, ToSql, Type}; + + let samples: Vec<Decimal> = test::black_box(SAMPLE_STRS.iter().map(|x| Decimal::from_str(x).unwrap()).collect()); + let t = Type::new("".into(), 0, Kind::Simple, "".into()); + let mut bytes: BytesMut = BytesMut::with_capacity(100).into(); + + b.iter(|| { + for _ in 0..100 { + for sample in &samples { + bytes.clear(); + sample.to_sql(&t, &mut bytes).unwrap(); + let result = Decimal::from_sql(&t, &bytes).unwrap(); + ::test::black_box(result); + } + } + }); +} + +#[cfg(feature = "maths")] +mod maths { + use rust_decimal::prelude::*; + + #[bench] + fn powi(b: &mut ::test::Bencher) { + // These exponents have to be fairly small because multiplcation overflows easily + let samples = &[ + (Decimal::from_str("36.7").unwrap(), 5), + (Decimal::from_str("0.00000007").unwrap(), 5), + (Decimal::from(2), 64), + (Decimal::from_str("8819287.19276555").unwrap(), 3), + (Decimal::from_str("-8819287.19276555").unwrap(), 3), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.0.powi(sample.1); + ::test::black_box(result); + } + }); + } + + #[bench] + fn sqrt(b: &mut ::test::Bencher) { + let samples = &[ + Decimal::from_str("36.7").unwrap(), + Decimal::from_str("0.00000007").unwrap(), + Decimal::from(2), + Decimal::from_str("8819287.19276555").unwrap(), + Decimal::from_str("-8819287.19276555").unwrap(), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.sqrt(); + ::test::black_box(result); + } + }); + } + + #[bench] + fn exp(b: &mut ::test::Bencher) { + let samples = &[ + Decimal::from_str("3.7").unwrap(), + Decimal::from_str("0.07").unwrap(), + Decimal::from(2), + Decimal::from_str("8.19").unwrap(), + Decimal::from_str("-8.19").unwrap(), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.exp(); + ::test::black_box(result); + } + }); + } + + #[bench] + fn norm_cdf(b: &mut ::test::Bencher) { + let samples = &[ + Decimal::from_str("3.7").unwrap(), + Decimal::from_str("0.007").unwrap(), + Decimal::from(2), + Decimal::from_str("1.19").unwrap(), + Decimal::from_str("-1.19").unwrap(), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.norm_cdf(); + ::test::black_box(result); + } + }); + } + + #[bench] + fn norm_pdf(b: &mut ::test::Bencher) { + let samples = &[ + Decimal::from_str("3.7").unwrap(), + Decimal::from_str("0.007").unwrap(), + Decimal::from(2), + Decimal::from_str("1.19").unwrap(), + Decimal::from_str("-1.19").unwrap(), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.norm_pdf(); + ::test::black_box(result); + } + }); + } + + #[bench] + fn ln(b: &mut ::test::Bencher) { + let samples = &[ + Decimal::from_str("36.7").unwrap(), + Decimal::from_str("0.00000007").unwrap(), + Decimal::from(2), + Decimal::from_str("8819287.19").unwrap(), + Decimal::from_str("-8819287.19").unwrap(), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.ln(); + ::test::black_box(result); + } + }); + } + + #[bench] + fn erf(b: &mut ::test::Bencher) { + let samples = &[ + Decimal::from(0), + Decimal::from(1), + Decimal::from_str("-0.98717").unwrap(), + Decimal::from_str("0.07").unwrap(), + Decimal::from_str("0.1111").unwrap(), + Decimal::from_str("0.4").unwrap(), + ]; + b.iter(|| { + for sample in samples.iter() { + let result = sample.erf(); + ::test::black_box(result); + } + }); + } +} |