|
| 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 serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 8 | +struct DateFixture(Vec<Test>); |
| 9 | + |
| 10 | +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] |
| 11 | +struct Test { |
| 12 | + year: i32, |
| 13 | + month: u8, |
| 14 | + day: u8, |
| 15 | + hour: u8, |
| 16 | + minute: u8, |
| 17 | + second: u8, |
| 18 | +} |
| 19 | + |
| 20 | +use criterion::{ |
| 21 | + black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, |
| 22 | +}; |
| 23 | +use icu_calendar::{ |
| 24 | + options::{DateAddOptions, Overflow}, |
| 25 | + types, AsCalendar, Calendar, Date, |
| 26 | +}; |
| 27 | + |
| 28 | +fn bench_date<A: AsCalendar>(date: &mut Date<A>) { |
| 29 | + // black_box used to avoid compiler optimization. |
| 30 | + // Arithmetic |
| 31 | + let mut options = DateAddOptions::default(); |
| 32 | + options.overflow = Some(Overflow::Constrain); |
| 33 | + date.try_add_with_options( |
| 34 | + types::DateDuration { |
| 35 | + is_negative: false, |
| 36 | + years: black_box(1), |
| 37 | + months: black_box(2), |
| 38 | + weeks: black_box(3), |
| 39 | + days: black_box(4), |
| 40 | + }, |
| 41 | + options, |
| 42 | + ) |
| 43 | + .unwrap(); |
| 44 | + |
| 45 | + // Retrieving vals |
| 46 | + let _ = black_box(date.year()); |
| 47 | + let _ = black_box(date.month()); |
| 48 | + let _ = black_box(date.day_of_month()); |
| 49 | + |
| 50 | + // Conversion to ISO. |
| 51 | + let _ = black_box(date.to_calendar(icu::calendar::cal::Iso)); |
| 52 | +} |
| 53 | + |
| 54 | +fn bench_calendar<C: Clone + Calendar>( |
| 55 | + group: &mut BenchmarkGroup<WallTime>, |
| 56 | + name: &str, |
| 57 | + fxs: &DateFixture, |
| 58 | + calendar: C, |
| 59 | + calendar_date_init: impl Fn(i32, u8, u8, C) -> Date<C>, |
| 60 | +) { |
| 61 | + group.bench_function(name, |b| { |
| 62 | + b.iter(|| { |
| 63 | + for fx in &fxs.0 { |
| 64 | + // Instantion from int |
| 65 | + let mut instantiated_date_calendar = |
| 66 | + calendar_date_init(fx.year, fx.month, fx.day, calendar.clone()); |
| 67 | + |
| 68 | + // Conversion from ISO |
| 69 | + let date_iso = Date::try_new_iso(fx.year, fx.month, fx.day).unwrap(); |
| 70 | + let mut converted_date_calendar = date_iso.to_calendar(calendar.clone()); |
| 71 | + |
| 72 | + bench_date(&mut instantiated_date_calendar); |
| 73 | + bench_date(&mut converted_date_calendar); |
| 74 | + } |
| 75 | + }) |
| 76 | + }); |
| 77 | +} |
| 78 | + |
| 79 | +fn date_benches(c: &mut Criterion) { |
| 80 | + let mut group = c.benchmark_group("date"); |
| 81 | + let fxs = serde_json::from_str::<DateFixture>(include_str!("fixtures/datetimes.json")).unwrap(); |
| 82 | + |
| 83 | + bench_calendar( |
| 84 | + &mut group, |
| 85 | + "calendar/overview", |
| 86 | + &fxs, |
| 87 | + icu::calendar::cal::Iso, |
| 88 | + |y, m, d, _| Date::try_new_iso(y, m, d).unwrap(), |
| 89 | + ); |
| 90 | + |
| 91 | + bench_calendar( |
| 92 | + &mut group, |
| 93 | + "calendar/buddhist", |
| 94 | + &fxs, |
| 95 | + icu::calendar::cal::Buddhist, |
| 96 | + |y, m, d, _| Date::try_new_buddhist(y, m, d).unwrap(), |
| 97 | + ); |
| 98 | + |
| 99 | + bench_calendar( |
| 100 | + &mut group, |
| 101 | + "calendar/coptic", |
| 102 | + &fxs, |
| 103 | + icu::calendar::cal::Coptic, |
| 104 | + |y, m, d, _| Date::try_new_coptic(y, m, d).unwrap(), |
| 105 | + ); |
| 106 | + |
| 107 | + bench_calendar( |
| 108 | + &mut group, |
| 109 | + "calendar/ethiopic", |
| 110 | + &fxs, |
| 111 | + icu::calendar::cal::Ethiopian::new(), |
| 112 | + |y, m, d, _| { |
| 113 | + Date::try_new_ethiopian(icu::calendar::cal::EthiopianEraStyle::AmeteMihret, y, m, d) |
| 114 | + .unwrap() |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + bench_calendar( |
| 119 | + &mut group, |
| 120 | + "calendar/indian", |
| 121 | + &fxs, |
| 122 | + icu::calendar::cal::Indian, |
| 123 | + |y, m, d, _| Date::try_new_indian(y, m, d).unwrap(), |
| 124 | + ); |
| 125 | + |
| 126 | + bench_calendar( |
| 127 | + &mut group, |
| 128 | + "calendar/persian", |
| 129 | + &fxs, |
| 130 | + icu::calendar::cal::Persian, |
| 131 | + |y, m, d, _| Date::try_new_persian(y, m, d).unwrap(), |
| 132 | + ); |
| 133 | + |
| 134 | + bench_calendar( |
| 135 | + &mut group, |
| 136 | + "calendar/roc", |
| 137 | + &fxs, |
| 138 | + icu::calendar::cal::Roc, |
| 139 | + |y, m, d, _| Date::try_new_roc(y, m, d).unwrap(), |
| 140 | + ); |
| 141 | + |
| 142 | + bench_calendar( |
| 143 | + &mut group, |
| 144 | + "calendar/julian", |
| 145 | + &fxs, |
| 146 | + icu::calendar::cal::Julian, |
| 147 | + |y, m, d, _| Date::try_new_julian(y, m, d).unwrap(), |
| 148 | + ); |
| 149 | + |
| 150 | + bench_calendar( |
| 151 | + &mut group, |
| 152 | + "calendar/chinese_cached", |
| 153 | + &fxs, |
| 154 | + icu::calendar::cal::ChineseTraditional::new(), |
| 155 | + |y, m, d, _| Date::try_new_chinese_traditional(y, types::Month::new(m), d).unwrap(), |
| 156 | + ); |
| 157 | + |
| 158 | + bench_calendar( |
| 159 | + &mut group, |
| 160 | + "calendar/dangi_cached", |
| 161 | + &fxs, |
| 162 | + icu::calendar::cal::KoreanTraditional::new(), |
| 163 | + |y, m, d, _| Date::try_new_korean_traditional(y, types::Month::new(m), d).unwrap(), |
| 164 | + ); |
| 165 | + |
| 166 | + bench_calendar( |
| 167 | + &mut group, |
| 168 | + "calendar/hebrew", |
| 169 | + &fxs, |
| 170 | + icu::calendar::cal::Hebrew, |
| 171 | +<<<<<<< HEAD |
| 172 | + |y, m, d, _c| Date::try_new_hebrew_v2(y, m.into(), d).unwrap(), |
| 173 | +||||||| 8433fc5a55 (agent-driven: Use month.into() instead of Month::new()) |
| 174 | + |y, m, d, c| Date::try_new(types::YearInput::Extended(y), m.into(), d, c).unwrap(), |
| 175 | +======= |
| 176 | + |y, m, d, c| { |
| 177 | + Date::try_new(types::YearInput::Extended(y), types::Month::new(m), d, c).unwrap() |
| 178 | + }, |
| 179 | +>>>>>>> parent of 8433fc5a55 (agent-driven: Use month.into() instead of Month::new()) |
| 180 | + ); |
| 181 | + |
| 182 | + bench_calendar( |
| 183 | + &mut group, |
| 184 | + "calendar/gregorian", |
| 185 | + &fxs, |
| 186 | + icu::calendar::cal::Gregorian, |
| 187 | + |y, m, d, _| Date::try_new_gregorian(y, m, d).unwrap(), |
| 188 | + ); |
| 189 | + |
| 190 | + bench_calendar( |
| 191 | + &mut group, |
| 192 | + "calendar/islamic/civil", |
| 193 | + &fxs, |
| 194 | + icu::calendar::cal::Hijri::new_tabular( |
| 195 | + icu::calendar::cal::hijri::TabularAlgorithmLeapYears::TypeII, |
| 196 | + icu::calendar::cal::hijri::TabularAlgorithmEpoch::Friday, |
| 197 | + ), |
| 198 | + |y, m, d, c| Date::try_new_hijri_with_calendar(y, m, d, c).unwrap(), |
| 199 | + ); |
| 200 | + |
| 201 | + bench_calendar( |
| 202 | + &mut group, |
| 203 | + "calendar/islamic/tabular", |
| 204 | + &fxs, |
| 205 | + icu::calendar::cal::Hijri::new_tabular( |
| 206 | + icu::calendar::cal::hijri::TabularAlgorithmLeapYears::TypeII, |
| 207 | + icu::calendar::cal::hijri::TabularAlgorithmEpoch::Thursday, |
| 208 | + ), |
| 209 | + |y, m, d, c| Date::try_new_hijri_with_calendar(y, m, d, c).unwrap(), |
| 210 | + ); |
| 211 | + |
| 212 | + bench_calendar( |
| 213 | + &mut group, |
| 214 | + "calendar/islamic/ummalqura", |
| 215 | + &fxs, |
| 216 | + icu::calendar::cal::Hijri::new_umm_al_qura(), |
| 217 | + |y, m, d, c| Date::try_new_hijri_with_calendar(y, m, d, c).unwrap(), |
| 218 | + ); |
| 219 | + |
| 220 | + #[allow(deprecated)] |
| 221 | + bench_calendar( |
| 222 | + &mut group, |
| 223 | + "calendar/islamic/observational", |
| 224 | + &fxs, |
| 225 | + icu::calendar::cal::Hijri::new_simulated_mecca(), |
| 226 | + |y, m, d, c| Date::try_new_hijri_with_calendar(y, m, d, c).unwrap(), |
| 227 | + ); |
| 228 | + |
| 229 | + group.finish(); |
| 230 | +} |
| 231 | + |
| 232 | +criterion_group!(benches, date_benches); |
| 233 | +criterion_main!(benches); |
0 commit comments