forked from chronotope/chrono
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchrono.rs
250 lines (226 loc) · 8.18 KB
/
chrono.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//! Benchmarks for chrono that just depend on std
use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use chrono::format::StrftimeItems;
use chrono::prelude::*;
#[cfg(feature = "unstable-locales")]
use chrono::Locale;
use chrono::{DateTime, FixedOffset, Local, TimeDelta, Utc, __BenchYearFlags};
fn bench_date_from_ymd(c: &mut Criterion) {
c.bench_function("bench_date_from_ymd", |b| {
let expected = NaiveDate::from_ymd_opt(2024, 2, 12);
b.iter(|| {
let (y, m, d) = black_box((2024, 2, 12));
assert_eq!(NaiveDate::from_ymd_opt(y, m, d), expected)
})
});
}
fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
b.iter(|| {
let str = black_box("Wed, 18 Feb 2015 23:16:09 +0000");
DateTime::parse_from_rfc2822(str).unwrap()
})
});
}
fn bench_datetime_parse_from_rfc3339(c: &mut Criterion) {
c.bench_function("bench_datetime_parse_from_rfc3339", |b| {
b.iter(|| {
let str = black_box("2015-02-18T23:59:60.234567+05:00");
DateTime::parse_from_rfc3339(str).unwrap()
})
});
}
fn bench_datetime_from_str(c: &mut Criterion) {
c.bench_function("bench_datetime_from_str", |b| {
b.iter(|| {
use std::str::FromStr;
let str = black_box("2019-03-30T18:46:57.193Z");
DateTime::<Utc>::from_str(str).unwrap()
})
});
}
fn bench_datetime_to_rfc2822(c: &mut Criterion) {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
}
fn bench_datetime_to_rfc3339(c: &mut Criterion) {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
}
fn bench_datetime_to_rfc3339_opts(c: &mut Criterion) {
let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap();
let dt = pst
.from_local_datetime(
&NaiveDate::from_ymd_opt(2018, 1, 11)
.unwrap()
.and_hms_nano_opt(10, 5, 13, 84_660_000)
.unwrap(),
)
.unwrap();
c.bench_function("bench_datetime_to_rfc3339_opts", |b| {
b.iter(|| black_box(dt).to_rfc3339_opts(SecondsFormat::Nanos, true))
});
}
fn bench_year_flags_from_year(c: &mut Criterion) {
c.bench_function("bench_year_flags_from_year", |b| {
b.iter(|| {
for year in -999i32..1000 {
let _ = __BenchYearFlags::from_year(black_box(year));
}
})
});
}
fn bench_get_local_time(c: &mut Criterion) {
c.bench_function("bench_get_local_time", |b| {
b.iter(|| {
let _ = Local::now();
})
});
}
/// Returns the number of multiples of `div` in the range `start..end`.
///
/// If the range `start..end` is back-to-front, i.e. `start` is greater than `end`, the
/// behaviour is defined by the following equation:
/// `in_between(start, end, div) == - in_between(end, start, div)`.
///
/// When `div` is 1, this is equivalent to `end - start`, i.e. the length of `start..end`.
///
/// # Panics
///
/// Panics if `div` is not positive.
fn in_between(start: i32, end: i32, div: i32) -> i32 {
assert!(div > 0, "in_between: nonpositive div = {}", div);
let start = (start.div_euclid(div), start.rem_euclid(div));
let end = (end.div_euclid(div), end.rem_euclid(div));
// The lowest multiple of `div` greater than or equal to `start`, divided.
let start = start.0 + (start.1 != 0) as i32;
// The lowest multiple of `div` greater than or equal to `end`, divided.
let end = end.0 + (end.1 != 0) as i32;
end - start
}
/// Alternative implementation to `Datelike::num_days_from_ce`
fn num_days_from_ce_alt<Date: Datelike>(date: &Date) -> i32 {
let year = date.year();
let diff = move |div| in_between(1, year, div);
// 365 days a year, one more in leap years. In the gregorian calendar, leap years are all
// the multiples of 4 except multiples of 100 but including multiples of 400.
date.ordinal() as i32 + 365 * diff(1) + diff(4) - diff(100) + diff(400)
}
fn bench_num_days_from_ce(c: &mut Criterion) {
let mut group = c.benchmark_group("num_days_from_ce");
for year in &[1, 500, 2000, 2019] {
let d = NaiveDate::from_ymd_opt(*year, 1, 1).unwrap();
group.bench_with_input(BenchmarkId::new("new", year), &d, |b, y| {
b.iter(|| num_days_from_ce_alt(y))
});
group.bench_with_input(BenchmarkId::new("classic", year), &d, |b, y| {
b.iter(|| y.num_days_from_ce())
});
}
}
fn bench_parse_strftime(c: &mut Criterion) {
c.bench_function("bench_parse_strftime", |b| {
b.iter(|| {
let str = black_box("%a, %d %b %Y %H:%M:%S GMT");
let items = StrftimeItems::new(str);
black_box(items.collect::<Vec<_>>());
})
});
}
#[cfg(feature = "unstable-locales")]
fn bench_parse_strftime_localized(c: &mut Criterion) {
c.bench_function("bench_parse_strftime_localized", |b| {
b.iter(|| {
let str = black_box("%a, %d %b %Y %H:%M:%S GMT");
let items = StrftimeItems::new_with_locale(str, Locale::nl_NL);
black_box(items.collect::<Vec<_>>());
})
});
}
fn bench_format(c: &mut Criterion) {
let dt = Local::now();
c.bench_function("bench_format", |b| {
b.iter(|| format!("{}", black_box(dt).format("%Y-%m-%dT%H:%M:%S%.f%:z")))
});
}
fn bench_format_with_items(c: &mut Criterion) {
let dt = Local::now();
let items: Vec<_> = StrftimeItems::new("%Y-%m-%dT%H:%M:%S%.f%:z").collect();
c.bench_function("bench_format_with_items", |b| {
b.iter(|| format!("{}", black_box(dt).format_with_items(items.iter())))
});
}
fn bench_format_manual(c: &mut Criterion) {
let dt = Local::now();
c.bench_function("bench_format_manual", |b| {
b.iter(|| {
black_box(dt);
format!(
"{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:09}{:+02}:{:02}",
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second(),
dt.nanosecond(),
dt.offset().fix().local_minus_utc() / 3600,
dt.offset().fix().local_minus_utc() / 60,
)
})
});
}
fn bench_naivedate_add_signed(c: &mut Criterion) {
let date = NaiveDate::from_ymd_opt(2023, 7, 29).unwrap();
let extra = TimeDelta::try_days(25).unwrap();
c.bench_function("bench_naivedate_add_signed", |b| {
b.iter(|| black_box(date).checked_add_signed(extra).unwrap())
});
}
fn bench_datetime_with(c: &mut Criterion) {
let dt = FixedOffset::east_opt(3600).unwrap().with_ymd_and_hms(2023, 9, 23, 7, 36, 0).unwrap();
c.bench_function("bench_datetime_with", |b| {
b.iter(|| black_box(black_box(dt).with_hour(12)).unwrap())
});
}
criterion_group!(
benches,
bench_date_from_ymd,
bench_datetime_parse_from_rfc2822,
bench_datetime_parse_from_rfc3339,
bench_datetime_from_str,
bench_datetime_to_rfc2822,
bench_datetime_to_rfc3339,
bench_datetime_to_rfc3339_opts,
bench_year_flags_from_year,
bench_num_days_from_ce,
bench_get_local_time,
bench_parse_strftime,
bench_format,
bench_format_with_items,
bench_format_manual,
bench_naivedate_add_signed,
bench_datetime_with,
);
#[cfg(feature = "unstable-locales")]
criterion_group!(unstable_locales, bench_parse_strftime_localized,);
#[cfg(not(feature = "unstable-locales"))]
criterion_main!(benches);
#[cfg(feature = "unstable-locales")]
criterion_main!(benches, unstable_locales);