| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use core::str::FromStr; |
| 6 |
use rand::SeedableRng; |
| 7 |
use rand_distr::{Distribution, Triangular}; |
| 8 |
use rand_pcg::Lcg64Xsh32; |
| 9 |
|
| 10 |
use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
| 11 |
|
| 12 |
use fixed_decimal::Decimal; |
| 13 |
|
| 14 |
// TODO: move to helpers.rs |
| 15 |
fn triangular_nums(range: f64) -> Vec<isize> { |
| 16 |
// Use Lcg64Xsh32, a small, fast PRNG. |
| 17 |
// Generate 1000 numbers between -range and +range, weighted around 0. |
| 18 |
let rng = Lcg64Xsh32::seed_from_u64(2020); |
| 19 |
let dist = Triangular::new(-range, range, 0.0).unwrap(); |
| 20 |
dist.sample_iter(rng) |
| 21 |
.take(1000) |
| 22 |
.map(|v| v as isize) |
| 23 |
.collect() |
| 24 |
} |
| 25 |
|
| 26 |
// TODO: move to helpers.rs |
| 27 |
fn triangular_floats(range: f64) -> impl Iterator<Item = f64> { |
| 28 |
// Use Lcg64Xsh32, a small, fast PRNG.s |
| 29 |
// Generate 1000 numbers between -range and +range, weighted around 0. |
| 30 |
let rng = Lcg64Xsh32::seed_from_u64(2024); |
| 31 |
let dist = Triangular::new(-range, range, 0.0).unwrap(); |
| 32 |
dist.sample_iter(rng).take(1000) |
| 33 |
} |
| 34 |
|
| 35 |
fn overview_bench(c: &mut Criterion) { |
| 36 |
let nums = triangular_nums(1e4); |
| 37 |
let values: Vec<_> = nums.iter().map(|n| n.to_string()).collect(); |
| 38 |
c.bench_function("fixed_decimal/overview", |b| { |
| 39 |
#[expect(clippy::suspicious_map)] |
| 40 |
b.iter(|| { |
| 41 |
// This benchmark focuses on short numbers and performs: |
| 42 |
// * Construction of FixedDecimals from isize |
| 43 |
// * Construction of FixedDecimals from strings |
| 44 |
// * Serialization of FixedDecimal to string |
| 45 |
nums.iter() |
| 46 |
.map(|v| black_box(*v)) |
| 47 |
.map(Decimal::from) |
| 48 |
.count(); |
| 49 |
let fds: Vec<_> = values |
| 50 |
.iter() |
| 51 |
.map(black_box) |
| 52 |
.map(|v| Decimal::from_str(v).expect("Failed to parse")) |
| 53 |
.collect(); |
| 54 |
fds.iter().map(black_box).map(|v| v.to_string()).count(); |
| 55 |
}); |
| 56 |
}); |
| 57 |
|
| 58 |
{ |
| 59 |
smaller_isize_benches(c); |
| 60 |
larger_isize_benches(c); |
| 61 |
to_string_benches(c); |
| 62 |
from_string_benches(c); |
| 63 |
rounding_benches(c); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
fn smaller_isize_benches(c: &mut Criterion) { |
| 68 |
// Smaller nums: -1e4 to 1e4 |
| 69 |
let nums = triangular_nums(1e4); |
| 70 |
|
| 71 |
// Note: this could be bench_function_with_inputs, but there are 1000 random inputs. |
| 72 |
// Instead, consider all inputs together in the same benchmark. |
| 73 |
c.bench_function("isize/smaller", |b| { |
| 74 |
b.iter(|| { |
| 75 |
nums.iter() |
| 76 |
.map(|v| black_box(*v)) |
| 77 |
.map(Decimal::from) |
| 78 |
.count() |
| 79 |
}); |
| 80 |
}); |
| 81 |
} |
| 82 |
|
| 83 |
fn larger_isize_benches(c: &mut Criterion) { |
| 84 |
// Larger nums: -1e16 to 1e16 |
| 85 |
let nums = triangular_nums(1e16); |
| 86 |
|
| 87 |
// Note: this could be bench_function_with_inputs, but there are 1000 random inputs. |
| 88 |
// Instead, consider all inputs together in the same benchmark. |
| 89 |
c.bench_function("isize/larger", |b| { |
| 90 |
b.iter(|| { |
| 91 |
nums.iter() |
| 92 |
.map(|v| black_box(*v)) |
| 93 |
.map(Decimal::from) |
| 94 |
.count() |
| 95 |
}); |
| 96 |
}); |
| 97 |
} |
| 98 |
|
| 99 |
fn to_string_benches(c: &mut Criterion) { |
| 100 |
use criterion::BenchmarkId; |
| 101 |
use writeable::Writeable; |
| 102 |
|
| 103 |
let objects = [ |
| 104 |
{ |
| 105 |
let mut fd = Decimal::from(2250); |
| 106 |
fd.multiply_pow10(-2); |
| 107 |
fd |
| 108 |
}, |
| 109 |
Decimal::from(908070605040302010u128), |
| 110 |
]; |
| 111 |
|
| 112 |
{ |
| 113 |
let mut group = c.benchmark_group("to_string/to_string"); |
| 114 |
for object in objects.iter() { |
| 115 |
group.bench_with_input( |
| 116 |
BenchmarkId::from_parameter(object.to_string()), |
| 117 |
object, |
| 118 |
|b, object| b.iter(|| object.to_string()), |
| 119 |
); |
| 120 |
} |
| 121 |
group.finish(); |
| 122 |
} |
| 123 |
|
| 124 |
{ |
| 125 |
let mut group = c.benchmark_group("to_string/write_to"); |
| 126 |
for object in objects.iter() { |
| 127 |
group.bench_with_input( |
| 128 |
BenchmarkId::from_parameter(object.to_string()), |
| 129 |
object, |
| 130 |
|b, object| b.iter(|| object.write_to_string().into_owned()), |
| 131 |
); |
| 132 |
} |
| 133 |
group.finish(); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
fn from_string_benches(c: &mut Criterion) { |
| 138 |
use criterion::BenchmarkId; |
| 139 |
|
| 140 |
let objects = [ |
| 141 |
"0012.3400", |
| 142 |
"00.0012216734340", |
| 143 |
"00002342561123400.0", |
| 144 |
"-00123400", |
| 145 |
"922337203685477580898230948203840239384.9823094820384023938423424", |
| 146 |
"0.000000001", |
| 147 |
"1000000001", |
| 148 |
&{ |
| 149 |
let mut x = format!("{:0fill$}", 0, fill = i16::MAX as usize + 1); |
| 150 |
x.push('.'); |
| 151 |
x.push_str(&format!("{:0fill$}", 0, fill = i16::MAX as usize + 1)); |
| 152 |
x |
| 153 |
}, |
| 154 |
]; |
| 155 |
|
| 156 |
{ |
| 157 |
let mut group = c.benchmark_group("from_string"); |
| 158 |
for object in objects.iter() { |
| 159 |
group.bench_with_input( |
| 160 |
BenchmarkId::from_parameter(object.to_string()), |
| 161 |
object, |
| 162 |
|b, object| b.iter(|| Decimal::from_str(object).unwrap()), |
| 163 |
); |
| 164 |
} |
| 165 |
group.finish(); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
fn rounding_benches(c: &mut Criterion) { |
| 170 |
use fixed_decimal::{FloatPrecision, SignedRoundingMode, UnsignedRoundingMode}; |
| 171 |
const ROUNDING_MODES: [(&str, SignedRoundingMode); 9] = [ |
| 172 |
("ceil", SignedRoundingMode::Ceil), |
| 173 |
("floor", SignedRoundingMode::Floor), |
| 174 |
( |
| 175 |
"expand", |
| 176 |
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Expand), |
| 177 |
), |
| 178 |
( |
| 179 |
"trunc", |
| 180 |
SignedRoundingMode::Unsigned(UnsignedRoundingMode::Trunc), |
| 181 |
), |
| 182 |
("half_ceil", SignedRoundingMode::HalfCeil), |
| 183 |
("half_floor", SignedRoundingMode::HalfFloor), |
| 184 |
( |
| 185 |
"half_expand", |
| 186 |
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfExpand), |
| 187 |
), |
| 188 |
( |
| 189 |
"half_trunc", |
| 190 |
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfTrunc), |
| 191 |
), |
| 192 |
( |
| 193 |
"half_even", |
| 194 |
SignedRoundingMode::Unsigned(UnsignedRoundingMode::HalfEven), |
| 195 |
), |
| 196 |
]; |
| 197 |
|
| 198 |
let nums: Vec<_> = triangular_floats(1e7) |
| 199 |
.map(|f| Decimal::try_from_f64(f, FloatPrecision::RoundTrip).unwrap()) |
| 200 |
.collect(); |
| 201 |
let mut group = c.benchmark_group("rounding"); |
| 202 |
|
| 203 |
for (name, rounding_mode) in ROUNDING_MODES { |
| 204 |
group.bench_function(name, |b| { |
| 205 |
b.iter(|| { |
| 206 |
for offset in -5..=5 { |
| 207 |
nums.iter() |
| 208 |
.cloned() |
| 209 |
.map(|num| { |
| 210 |
Decimal::rounded_with_mode(black_box(num), offset, rounding_mode) |
| 211 |
}) |
| 212 |
.for_each(|num| { |
| 213 |
black_box(num); |
| 214 |
}); |
| 215 |
} |
| 216 |
}) |
| 217 |
}); |
| 218 |
} |
| 219 |
|
| 220 |
group.finish() |
| 221 |
} |
| 222 |
|
| 223 |
criterion_group!(benches, overview_bench,); |
| 224 |
criterion_main!(benches); |