| 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::{ |
| 8 |
black_box, criterion_group, criterion_main, measurement::WallTime, BenchmarkGroup, Criterion, |
| 9 |
}; |
| 10 |
use fixtures::DateFixture; |
| 11 |
use icu_calendar::{AsCalendar, Calendar, Date, DateDuration}; |
| 12 |
|
| 13 |
fn bench_date<A: AsCalendar>(date: &mut Date<A>) { |
| 14 |
// black_box used to avoid compiler optimization. |
| 15 |
// Arithmetic |
| 16 |
date.add(DateDuration::new( |
| 17 |
black_box(1), |
| 18 |
black_box(2), |
| 19 |
black_box(3), |
| 20 |
black_box(4), |
| 21 |
)); |
| 22 |
|
| 23 |
// Retrieving vals |
| 24 |
let _ = black_box(date.year().number); |
| 25 |
let _ = black_box(date.month().ordinal); |
| 26 |
let _ = black_box(date.day_of_month().0); |
| 27 |
|
| 28 |
// Conversion to ISO. |
| 29 |
let _ = black_box(date.to_iso()); |
| 30 |
} |
| 31 |
|
| 32 |
fn bench_calendar<C: Clone + Calendar>( |
| 33 |
group: &mut BenchmarkGroup<WallTime>, |
| 34 |
name: &str, |
| 35 |
fxs: &DateFixture, |
| 36 |
calendar: C, |
| 37 |
calendar_date_init: impl Fn(i32, u8, u8) -> Date<C>, |
| 38 |
) { |
| 39 |
group.bench_function(name, |b| { |
| 40 |
b.iter(|| { |
| 41 |
for fx in &fxs.0 { |
| 42 |
// Instantion from int |
| 43 |
let mut instantiated_date_calendar = calendar_date_init(fx.year, fx.month, fx.day); |
| 44 |
|
| 45 |
// Conversion from ISO |
| 46 |
let date_iso = Date::try_new_iso_date(fx.year, fx.month, fx.day).unwrap(); |
| 47 |
let mut converted_date_calendar = Date::new_from_iso(date_iso, calendar.clone()); |
| 48 |
|
| 49 |
bench_date(&mut instantiated_date_calendar); |
| 50 |
bench_date(&mut converted_date_calendar); |
| 51 |
} |
| 52 |
}) |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
fn date_benches(c: &mut Criterion) { |
| 57 |
let mut group = c.benchmark_group("date"); |
| 58 |
let fxs = serde_json::from_str::<DateFixture>(include_str!("fixtures/datetimes.json")).unwrap(); |
| 59 |
|
| 60 |
bench_calendar( |
| 61 |
&mut group, |
| 62 |
"calendar/overview", |
| 63 |
&fxs, |
| 64 |
icu::calendar::iso::Iso, |
| 65 |
|y, m, d| Date::try_new_iso_date(y, m, d).unwrap(), |
| 66 |
); |
| 67 |
|
| 68 |
#[cfg(feature = "bench")] |
| 69 |
bench_calendar( |
| 70 |
&mut group, |
| 71 |
"calendar/buddhist", |
| 72 |
&fxs, |
| 73 |
icu::calendar::buddhist::Buddhist, |
| 74 |
|y, m, d| Date::try_new_buddhist_date(y, m, d).unwrap(), |
| 75 |
); |
| 76 |
|
| 77 |
#[cfg(feature = "bench")] |
| 78 |
bench_calendar( |
| 79 |
&mut group, |
| 80 |
"calendar/coptic", |
| 81 |
&fxs, |
| 82 |
icu::calendar::coptic::Coptic, |
| 83 |
|y, m, d| Date::try_new_coptic_date(y, m, d).unwrap(), |
| 84 |
); |
| 85 |
|
| 86 |
#[cfg(feature = "bench")] |
| 87 |
bench_calendar( |
| 88 |
&mut group, |
| 89 |
"calendar/ethiopic", |
| 90 |
&fxs, |
| 91 |
icu::calendar::ethiopian::Ethiopian::new(), |
| 92 |
|y, m, d| { |
| 93 |
Date::try_new_ethiopian_date( |
| 94 |
icu::calendar::ethiopian::EthiopianEraStyle::AmeteMihret, |
| 95 |
y, |
| 96 |
m, |
| 97 |
d, |
| 98 |
) |
| 99 |
.unwrap() |
| 100 |
}, |
| 101 |
); |
| 102 |
|
| 103 |
#[cfg(feature = "bench")] |
| 104 |
bench_calendar( |
| 105 |
&mut group, |
| 106 |
"calendar/indian", |
| 107 |
&fxs, |
| 108 |
icu::calendar::indian::Indian, |
| 109 |
|y, m, d| Date::try_new_indian_date(y, m, d).unwrap(), |
| 110 |
); |
| 111 |
|
| 112 |
#[cfg(feature = "bench")] |
| 113 |
bench_calendar( |
| 114 |
&mut group, |
| 115 |
"calendar/persian", |
| 116 |
&fxs, |
| 117 |
icu::calendar::persian::Persian, |
| 118 |
|y, m, d| Date::try_new_persian_date(y, m, d).unwrap(), |
| 119 |
); |
| 120 |
|
| 121 |
#[cfg(feature = "bench")] |
| 122 |
bench_calendar( |
| 123 |
&mut group, |
| 124 |
"calendar/roc", |
| 125 |
&fxs, |
| 126 |
icu::calendar::roc::Roc, |
| 127 |
|y, m, d| Date::try_new_roc_date(y, m, d).unwrap(), |
| 128 |
); |
| 129 |
|
| 130 |
#[cfg(feature = "bench")] |
| 131 |
bench_calendar( |
| 132 |
&mut group, |
| 133 |
"calendar/julian", |
| 134 |
&fxs, |
| 135 |
icu::calendar::julian::Julian, |
| 136 |
|y, m, d| Date::try_new_julian_date(y, m, d).unwrap(), |
| 137 |
); |
| 138 |
|
| 139 |
#[cfg(feature = "bench")] |
| 140 |
bench_calendar( |
| 141 |
&mut group, |
| 142 |
"calendar/chinese_calculating", |
| 143 |
&fxs, |
| 144 |
icu::calendar::chinese::Chinese::new_always_calculating(), |
| 145 |
|y, m, d| { |
| 146 |
Date::try_new_chinese_date_with_calendar( |
| 147 |
y, |
| 148 |
m, |
| 149 |
d, |
| 150 |
icu::calendar::chinese::Chinese::new_always_calculating(), |
| 151 |
) |
| 152 |
.unwrap() |
| 153 |
}, |
| 154 |
); |
| 155 |
|
| 156 |
#[cfg(feature = "bench")] |
| 157 |
bench_calendar( |
| 158 |
&mut group, |
| 159 |
"calendar/chinese_cached", |
| 160 |
&fxs, |
| 161 |
icu::calendar::chinese::Chinese::new(), |
| 162 |
|y, m, d| { |
| 163 |
Date::try_new_chinese_date_with_calendar( |
| 164 |
y, |
| 165 |
m, |
| 166 |
d, |
| 167 |
icu::calendar::chinese::Chinese::new(), |
| 168 |
) |
| 169 |
.unwrap() |
| 170 |
}, |
| 171 |
); |
| 172 |
|
| 173 |
#[cfg(feature = "bench")] |
| 174 |
bench_calendar( |
| 175 |
&mut group, |
| 176 |
"calendar/dangi_calculating", |
| 177 |
&fxs, |
| 178 |
icu::calendar::dangi::Dangi::new_always_calculating(), |
| 179 |
|y, m, d| { |
| 180 |
Date::try_new_dangi_date_with_calendar( |
| 181 |
y, |
| 182 |
m, |
| 183 |
d, |
| 184 |
icu::calendar::dangi::Dangi::new_always_calculating(), |
| 185 |
) |
| 186 |
.unwrap() |
| 187 |
}, |
| 188 |
); |
| 189 |
|
| 190 |
#[cfg(feature = "bench")] |
| 191 |
bench_calendar( |
| 192 |
&mut group, |
| 193 |
"calendar/dangi_cached", |
| 194 |
&fxs, |
| 195 |
icu::calendar::dangi::Dangi::new(), |
| 196 |
|y, m, d| { |
| 197 |
Date::try_new_dangi_date_with_calendar(y, m, d, icu::calendar::dangi::Dangi::new()) |
| 198 |
.unwrap() |
| 199 |
}, |
| 200 |
); |
| 201 |
|
| 202 |
#[cfg(feature = "bench")] |
| 203 |
bench_calendar( |
| 204 |
&mut group, |
| 205 |
"calendar/hebrew", |
| 206 |
&fxs, |
| 207 |
icu::calendar::hebrew::Hebrew, |
| 208 |
|y, m, d| Date::try_new_hebrew_date(y, m, d).unwrap(), |
| 209 |
); |
| 210 |
|
| 211 |
#[cfg(feature = "bench")] |
| 212 |
bench_calendar( |
| 213 |
&mut group, |
| 214 |
"calendar/gregorian", |
| 215 |
&fxs, |
| 216 |
icu::calendar::gregorian::Gregorian, |
| 217 |
|y, m, d| Date::try_new_gregorian_date(y, m, d).unwrap(), |
| 218 |
); |
| 219 |
|
| 220 |
#[cfg(feature = "bench")] |
| 221 |
bench_calendar( |
| 222 |
&mut group, |
| 223 |
"calendar/islamic/civil", |
| 224 |
&fxs, |
| 225 |
icu::calendar::islamic::IslamicCivil::new(), |
| 226 |
|y, m, d| { |
| 227 |
Date::try_new_islamic_civil_date_with_calendar( |
| 228 |
y, |
| 229 |
m, |
| 230 |
d, |
| 231 |
icu::calendar::islamic::IslamicCivil::new(), |
| 232 |
) |
| 233 |
.unwrap() |
| 234 |
}, |
| 235 |
); |
| 236 |
|
| 237 |
#[cfg(feature = "bench")] |
| 238 |
bench_calendar( |
| 239 |
&mut group, |
| 240 |
"calendar/islamic/tabular", |
| 241 |
&fxs, |
| 242 |
icu::calendar::islamic::IslamicTabular::new(), |
| 243 |
|y, m, d| { |
| 244 |
Date::try_new_islamic_tabular_date_with_calendar( |
| 245 |
y, |
| 246 |
m, |
| 247 |
d, |
| 248 |
icu::calendar::islamic::IslamicTabular::new(), |
| 249 |
) |
| 250 |
.unwrap() |
| 251 |
}, |
| 252 |
); |
| 253 |
|
| 254 |
#[cfg(feature = "bench")] |
| 255 |
bench_calendar( |
| 256 |
&mut group, |
| 257 |
"calendar/islamic/ummalqura", |
| 258 |
&fxs, |
| 259 |
icu::calendar::islamic::IslamicUmmAlQura::new_always_calculating(), |
| 260 |
|y, m, d| { |
| 261 |
Date::try_new_ummalqura_date( |
| 262 |
y, |
| 263 |
m, |
| 264 |
d, |
| 265 |
icu::calendar::islamic::IslamicUmmAlQura::new_always_calculating(), |
| 266 |
) |
| 267 |
.unwrap() |
| 268 |
}, |
| 269 |
); |
| 270 |
|
| 271 |
#[cfg(feature = "bench")] |
| 272 |
bench_calendar( |
| 273 |
&mut group, |
| 274 |
"calendar/islamic/observational", |
| 275 |
&fxs, |
| 276 |
icu::calendar::islamic::IslamicObservational::new_always_calculating(), |
| 277 |
|y, m, d| { |
| 278 |
Date::try_new_observational_islamic_date( |
| 279 |
y, |
| 280 |
m, |
| 281 |
d, |
| 282 |
icu::calendar::islamic::IslamicObservational::new_always_calculating(), |
| 283 |
) |
| 284 |
.unwrap() |
| 285 |
}, |
| 286 |
); |
| 287 |
|
| 288 |
group.finish(); |
| 289 |
} |
| 290 |
|
| 291 |
criterion_group!(benches, date_benches); |
| 292 |
criterion_main!(benches); |