| 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 criterion::{ |
| 6 |
BenchmarkGroup, Criterion, black_box, criterion_group, criterion_main, measurement::WallTime, |
| 7 |
}; |
| 8 |
use icu_calendar::options::{DateDifferenceOptions, DateDurationUnit}; |
| 9 |
use icu_calendar::types::RataDie; |
| 10 |
use icu_calendar::{Calendar, Date}; |
| 11 |
|
| 12 |
#[macro_use] |
| 13 |
mod common; |
| 14 |
|
| 15 |
fn bench_calendar<C: Copy + Calendar>( |
| 16 |
group: &mut BenchmarkGroup<WallTime>, |
| 17 |
name: &str, |
| 18 |
calendar: C, |
| 19 |
unit: DateDurationUnit, |
| 20 |
) { |
| 21 |
// 300000 isn't a huge range, but these benchmarks are too slow otherwise. |
| 22 |
// |
| 23 |
// We can add more benchmarks once things are somewhat optimized |
| 24 |
let very_far_past = Date::from_rata_die(RataDie::new(-300_000_000), calendar); |
| 25 |
let far_past = Date::from_rata_die(RataDie::new(-300_000), calendar); |
| 26 |
let now = Date::from_rata_die(RataDie::new(0), calendar); |
| 27 |
let mut options = DateDifferenceOptions::default(); |
| 28 |
options.largest_unit = Some(unit); |
| 29 |
group.bench_function(format!("{name}/far_past"), |b| { |
| 30 |
b.iter(|| black_box(far_past.try_until_with_options(&now, options).unwrap())) |
| 31 |
}); |
| 32 |
// Still slow :/ |
| 33 |
if unit != DateDurationUnit::Months { |
| 34 |
group.bench_function(format!("{name}/very_far_past"), |b| { |
| 35 |
b.iter(|| black_box(very_far_past.try_until_with_options(&now, options).unwrap())) |
| 36 |
}); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
fn until_benches(c: &mut Criterion) { |
| 41 |
let mut group = c.benchmark_group("until/years"); |
| 42 |
bench_all_calendars!(group, bench_calendar, DateDurationUnit::Years); |
| 43 |
group.finish(); |
| 44 |
|
| 45 |
let mut group = c.benchmark_group("until/months"); |
| 46 |
bench_all_calendars!(group, bench_calendar, DateDurationUnit::Months); |
| 47 |
group.finish(); |
| 48 |
|
| 49 |
let mut group = c.benchmark_group("until/weeks"); |
| 50 |
bench_all_calendars!(group, bench_calendar, DateDurationUnit::Weeks); |
| 51 |
group.finish(); |
| 52 |
|
| 53 |
let mut group = c.benchmark_group("until/days"); |
| 54 |
bench_all_calendars!(group, bench_calendar, DateDurationUnit::Days); |
| 55 |
group.finish(); |
| 56 |
} |
| 57 |
|
| 58 |
criterion_group!(benches, until_benches); |
| 59 |
criterion_main!(benches); |