summaryrefslogtreecommitdiffstats
path: root/vendor/humantime/benches/datetime_format.rs
blob: 77d47667c0895c1aaa2b2a402273be261fb5c85e (plain)
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
#![feature(test)]
extern crate test;

use std::io::Write;
use std::time::{Duration, UNIX_EPOCH};

use humantime::format_rfc3339;

#[bench]
fn rfc3339_humantime_seconds(b: &mut test::Bencher) {
    let time = UNIX_EPOCH + Duration::new(1_483_228_799, 0);
    let mut buf = Vec::with_capacity(100);
    b.iter(|| {
        buf.truncate(0);
        write!(&mut buf, "{}", format_rfc3339(time)).unwrap()
    });
}

#[bench]
fn rfc3339_chrono(b: &mut test::Bencher) {
    use chrono::{DateTime, NaiveDateTime, Utc};
    use chrono::format::Item;
    use chrono::format::Item::*;
    use chrono::format::Numeric::*;
    use chrono::format::Fixed::*;
    use chrono::format::Pad::*;

    let time = DateTime::<Utc>::from_utc(
        NaiveDateTime::from_timestamp(1_483_228_799, 0), Utc);
    let mut buf = Vec::with_capacity(100);

    // formatting code from env_logger
    const ITEMS: &[Item<'static>] = {
        &[
            Numeric(Year, Zero),
            Literal("-"),
            Numeric(Month, Zero),
            Literal("-"),
            Numeric(Day, Zero),
            Literal("T"),
            Numeric(Hour, Zero),
            Literal(":"),
            Numeric(Minute, Zero),
            Literal(":"),
            Numeric(Second, Zero),
            Fixed(TimezoneOffsetZ),
        ]
    };


    b.iter(|| {
        buf.truncate(0);
        write!(&mut buf, "{}", time.format_with_items(ITEMS.iter().cloned()))
            .unwrap()
    });
}