Debian Codegraph @

debian/rust-vendor/icu_calendar/benches/date.rs

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