| 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 |
mod fixtures; |
| 6 |
|
| 7 |
use criterion::{Criterion, black_box, criterion_group, criterion_main}; |
| 8 |
use fixed_decimal::Decimal; |
| 9 |
use icu_plurals::PluralOperands; |
| 10 |
|
| 11 |
fn operands(c: &mut Criterion) { |
| 12 |
let data = |
| 13 |
serde_json::from_str::<fixtures::NumbersFixture>(include_str!("fixtures/numbers.json")) |
| 14 |
.expect("Failed to read a fixture"); |
| 15 |
|
| 16 |
c.bench_function("plurals/operands/overview", |b| { |
| 17 |
b.iter(|| { |
| 18 |
for s in &data.usize { |
| 19 |
let _: PluralOperands = black_box(*s).into(); |
| 20 |
} |
| 21 |
for s in &data.isize { |
| 22 |
let _: PluralOperands = black_box(*s).into(); |
| 23 |
} |
| 24 |
for s in &data.string { |
| 25 |
let _: PluralOperands = black_box(s) |
| 26 |
.parse() |
| 27 |
.expect("Failed to parse a number into an operands."); |
| 28 |
} |
| 29 |
for s in &data.fixed_decimals { |
| 30 |
let f: Decimal = { |
| 31 |
let mut dec = Decimal::from(s.value); |
| 32 |
dec.multiply_pow10(s.exponent); |
| 33 |
dec |
| 34 |
}; |
| 35 |
let _: PluralOperands = PluralOperands::from(black_box(&f)); |
| 36 |
} |
| 37 |
}) |
| 38 |
}); |
| 39 |
|
| 40 |
{ |
| 41 |
use criterion::BenchmarkId; |
| 42 |
|
| 43 |
c.bench_function("plurals/operands/create/usize", |b| { |
| 44 |
b.iter(|| { |
| 45 |
for s in &data.usize { |
| 46 |
let _: PluralOperands = black_box(*s).into(); |
| 47 |
} |
| 48 |
}) |
| 49 |
}); |
| 50 |
|
| 51 |
c.bench_function("plurals/operands/create/isize", |b| { |
| 52 |
b.iter(|| { |
| 53 |
for s in &data.isize { |
| 54 |
let _: PluralOperands = black_box(*s).into(); |
| 55 |
} |
| 56 |
}) |
| 57 |
}); |
| 58 |
|
| 59 |
c.bench_function("plurals/operands/create/string", |b| { |
| 60 |
b.iter(|| { |
| 61 |
for s in &data.string { |
| 62 |
let _: PluralOperands = black_box(s) |
| 63 |
.parse() |
| 64 |
.expect("Failed to parse a number into an operands."); |
| 65 |
} |
| 66 |
}) |
| 67 |
}); |
| 68 |
|
| 69 |
{ |
| 70 |
let mut group = c.benchmark_group("plurals/operands/create/string/samples"); |
| 71 |
for s in &data.string_samples { |
| 72 |
group.bench_with_input(BenchmarkId::from_parameter(s), s, |b, s| { |
| 73 |
b.iter(|| { |
| 74 |
let _: PluralOperands = black_box(s).parse().unwrap(); |
| 75 |
}) |
| 76 |
}); |
| 77 |
} |
| 78 |
} |
| 79 |
c.bench_function("plurals/operands/eq/mostly_unequal", |b| { |
| 80 |
let p: PluralOperands = 1.into(); |
| 81 |
b.iter(|| { |
| 82 |
for s in &data.isize { |
| 83 |
let q: PluralOperands = black_box(*s).into(); |
| 84 |
let _ = black_box(black_box(p) == black_box(q)); |
| 85 |
} |
| 86 |
}) |
| 87 |
}); |
| 88 |
|
| 89 |
c.bench_function("plurals/operands/eq/mostly_equal", |b| { |
| 90 |
b.iter(|| { |
| 91 |
for s in &data.isize { |
| 92 |
let p: PluralOperands = black_box(*s).into(); |
| 93 |
let q: PluralOperands = black_box(*s).into(); |
| 94 |
let _ = black_box(black_box(p) == black_box(q)); |
| 95 |
} |
| 96 |
}) |
| 97 |
}); |
| 98 |
|
| 99 |
c.bench_function("plurals/operands/create/from_fixed_decimal", |b| { |
| 100 |
b.iter(|| { |
| 101 |
for s in &data.fixed_decimals { |
| 102 |
let f: Decimal = { |
| 103 |
let mut dec = Decimal::from(s.value); |
| 104 |
dec.multiply_pow10(s.exponent); |
| 105 |
dec |
| 106 |
}; |
| 107 |
let _: PluralOperands = PluralOperands::from(black_box(&f)); |
| 108 |
} |
| 109 |
}); |
| 110 |
}); |
| 111 |
|
| 112 |
{ |
| 113 |
let samples = [ |
| 114 |
{ |
| 115 |
let mut dec = Decimal::from(1_i128); |
| 116 |
dec.multiply_pow10(0); |
| 117 |
dec |
| 118 |
}, |
| 119 |
{ |
| 120 |
let mut dec = Decimal::from(123450_i128); |
| 121 |
dec.multiply_pow10(-4); |
| 122 |
dec |
| 123 |
}, |
| 124 |
{ |
| 125 |
let mut dec = Decimal::from(2500_i128); |
| 126 |
dec.multiply_pow10(-2); |
| 127 |
dec |
| 128 |
}, |
| 129 |
]; |
| 130 |
let mut group = c.benchmark_group("plurals/operands/create/from_fixed_decimal/samples"); |
| 131 |
for s in samples.iter() { |
| 132 |
group.bench_with_input( |
| 133 |
BenchmarkId::from_parameter(format!("{:?}", s)), |
| 134 |
s, |
| 135 |
|b, f| b.iter(|| PluralOperands::from(black_box(f))), |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
criterion_group!(benches, operands); |
| 143 |
criterion_main!(benches); |