Debian Codegraph @

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