Skip to content

Commit

Permalink
Add benchmark for rounding methods of FixedDecimal (#4571)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 authored Feb 2, 2024
1 parent 42ab91e commit 6a96034
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion utils/fixed_decimal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ criterion = "0.4"

[features]
std = []
bench = []
bench = ["ryu"]
experimental = []
ryu = ["dep:ryu"]

Expand Down
49 changes: 49 additions & 0 deletions utils/fixed_decimal/benches/fixed_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ fn triangular_nums(range: f64) -> Vec<isize> {
.collect()
}

#[cfg(feature = "bench")]
fn triangular_floats(range: f64) -> impl Iterator<Item = f64> {
// Use Lcg64Xsh32, a small, fast PRNG.s
// Generate 1000 numbers between -range and +range, weighted around 0.
let rng = Lcg64Xsh32::seed_from_u64(2024);
let dist = Triangular::new(-range, range, 0.0).unwrap();
dist.sample_iter(rng).take(1000)
}

fn overview_bench(c: &mut Criterion) {
let nums = triangular_nums(1e4);
let values: Vec<_> = nums.iter().map(|n| n.to_string()).collect();
Expand Down Expand Up @@ -51,6 +60,7 @@ fn overview_bench(c: &mut Criterion) {
larger_isize_benches(c);
to_string_benches(c);
from_string_benches(c);
rounding_benches(c);
}
}

Expand Down Expand Up @@ -158,5 +168,44 @@ fn from_string_benches(c: &mut Criterion) {
}
}

#[cfg(feature = "bench")]
fn rounding_benches(c: &mut Criterion) {
use fixed_decimal::FloatPrecision;
#[allow(clippy::type_complexity)] // most compact representation in code
const ROUNDING_FNS: [(&str, fn(FixedDecimal, i16) -> FixedDecimal); 9] = [
("ceil", FixedDecimal::ceiled),
("floor", FixedDecimal::floored),
("expand", FixedDecimal::expanded),
("trunc", FixedDecimal::trunced),
("half_ceil", FixedDecimal::half_ceiled),
("half_floor", FixedDecimal::half_floored),
("half_expand", FixedDecimal::half_expanded),
("half_trunc", FixedDecimal::half_trunced),
("half_even", FixedDecimal::half_evened),
];

let nums: Vec<_> = triangular_floats(1e7)
.map(|f| FixedDecimal::try_from_f64(f, FloatPrecision::Floating).unwrap())
.collect();
let mut group = c.benchmark_group("rounding");

for (name, rounding_fn) in ROUNDING_FNS {
group.bench_function(name, |b| {
b.iter(|| {
for offset in -5..=5 {
nums.iter()
.cloned()
.map(|num| rounding_fn(black_box(num), offset))
.for_each(|num| {
black_box(num);
});
}
})
});
}

group.finish()
}

criterion_group!(benches, overview_bench,);
criterion_main!(benches);

0 comments on commit 6a96034

Please sign in to comment.