• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! Benchmarks for chrono that just depend on std
2 
3 extern crate chrono;
4 extern crate criterion;
5 
6 use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
7 
8 use chrono::prelude::*;
9 use chrono::{DateTime, FixedOffset, Utc, __BenchYearFlags};
10 
bench_datetime_parse_from_rfc2822(c: &mut Criterion)11 fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
12     c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
13         b.iter(|| {
14             let str = black_box("Wed, 18 Feb 2015 23:16:09 +0000");
15             DateTime::parse_from_rfc2822(str).unwrap()
16         })
17     });
18 }
19 
bench_datetime_parse_from_rfc3339(c: &mut Criterion)20 fn bench_datetime_parse_from_rfc3339(c: &mut Criterion) {
21     c.bench_function("bench_datetime_parse_from_rfc3339", |b| {
22         b.iter(|| {
23             let str = black_box("2015-02-18T23:59:60.234567+05:00");
24             DateTime::parse_from_rfc3339(str).unwrap()
25         })
26     });
27 }
28 
bench_datetime_from_str(c: &mut Criterion)29 fn bench_datetime_from_str(c: &mut Criterion) {
30     c.bench_function("bench_datetime_from_str", |b| {
31         b.iter(|| {
32             use std::str::FromStr;
33             let str = black_box("2019-03-30T18:46:57.193Z");
34             DateTime::<Utc>::from_str(str).unwrap()
35         })
36     });
37 }
38 
bench_datetime_to_rfc2822(c: &mut Criterion)39 fn bench_datetime_to_rfc2822(c: &mut Criterion) {
40     let pst = FixedOffset::east(8 * 60 * 60);
41     let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
42     c.bench_function("bench_datetime_to_rfc2822", |b| b.iter(|| black_box(dt).to_rfc2822()));
43 }
44 
bench_datetime_to_rfc3339(c: &mut Criterion)45 fn bench_datetime_to_rfc3339(c: &mut Criterion) {
46     let pst = FixedOffset::east(8 * 60 * 60);
47     let dt = pst.ymd(2018, 1, 11).and_hms_nano(10, 5, 13, 084_660_000);
48     c.bench_function("bench_datetime_to_rfc3339", |b| b.iter(|| black_box(dt).to_rfc3339()));
49 }
50 
bench_year_flags_from_year(c: &mut Criterion)51 fn bench_year_flags_from_year(c: &mut Criterion) {
52     c.bench_function("bench_year_flags_from_year", |b| {
53         b.iter(|| {
54             for year in -999i32..1000 {
55                 __BenchYearFlags::from_year(year);
56             }
57         })
58     });
59 }
60 
61 /// Returns the number of multiples of `div` in the range `start..end`.
62 ///
63 /// If the range `start..end` is back-to-front, i.e. `start` is greater than `end`, the
64 /// behaviour is defined by the following equation:
65 /// `in_between(start, end, div) == - in_between(end, start, div)`.
66 ///
67 /// When `div` is 1, this is equivalent to `end - start`, i.e. the length of `start..end`.
68 ///
69 /// # Panics
70 ///
71 /// Panics if `div` is not positive.
in_between(start: i32, end: i32, div: i32) -> i3272 fn in_between(start: i32, end: i32, div: i32) -> i32 {
73     assert!(div > 0, "in_between: nonpositive div = {}", div);
74     let start = (start.div_euclid(div), start.rem_euclid(div));
75     let end = (end.div_euclid(div), end.rem_euclid(div));
76     // The lowest multiple of `div` greater than or equal to `start`, divided.
77     let start = start.0 + (start.1 != 0) as i32;
78     // The lowest multiple of `div` greater than or equal to   `end`, divided.
79     let end = end.0 + (end.1 != 0) as i32;
80     end - start
81 }
82 
83 /// Alternative implementation to `Datelike::num_days_from_ce`
num_days_from_ce_alt<Date: Datelike>(date: &Date) -> i3284 fn num_days_from_ce_alt<Date: Datelike>(date: &Date) -> i32 {
85     let year = date.year();
86     let diff = move |div| in_between(1, year, div);
87     // 365 days a year, one more in leap years. In the gregorian calendar, leap years are all
88     // the multiples of 4 except multiples of 100 but including multiples of 400.
89     date.ordinal() as i32 + 365 * diff(1) + diff(4) - diff(100) + diff(400)
90 }
91 
bench_num_days_from_ce(c: &mut Criterion)92 fn bench_num_days_from_ce(c: &mut Criterion) {
93     let mut group = c.benchmark_group("num_days_from_ce");
94     for year in &[1, 500, 2000, 2019] {
95         let d = NaiveDate::from_ymd(*year, 1, 1);
96         group.bench_with_input(BenchmarkId::new("new", year), &d, |b, y| {
97             b.iter(|| num_days_from_ce_alt(y))
98         });
99         group.bench_with_input(BenchmarkId::new("classic", year), &d, |b, y| {
100             b.iter(|| y.num_days_from_ce())
101         });
102     }
103 }
104 
105 criterion_group!(
106     benches,
107     bench_datetime_parse_from_rfc2822,
108     bench_datetime_parse_from_rfc3339,
109     bench_datetime_from_str,
110     bench_datetime_to_rfc2822,
111     bench_datetime_to_rfc3339,
112     bench_year_flags_from_year,
113     bench_num_days_from_ce,
114 );
115 
116 criterion_main!(benches);
117