1#![feature(test)] 2extern crate test; 3 4use std::io::Write; 5use std::time::{Duration, UNIX_EPOCH}; 6 7use humantime::format_rfc3339; 8 9#[bench] 10fn rfc3339_humantime_seconds(b: &mut test::Bencher) { 11 let time = UNIX_EPOCH + Duration::new(1_483_228_799, 0); 12 let mut buf = Vec::with_capacity(100); 13 b.iter(|| { 14 buf.truncate(0); 15 write!(&mut buf, "{}", format_rfc3339(time)).unwrap() 16 }); 17} 18 19#[bench] 20fn rfc3339_chrono(b: &mut test::Bencher) { 21 use chrono::{DateTime, NaiveDateTime, Utc}; 22 use chrono::format::Item; 23 use chrono::format::Item::*; 24 use chrono::format::Numeric::*; 25 use chrono::format::Fixed::*; 26 use chrono::format::Pad::*; 27 28 let time = DateTime::<Utc>::from_utc( 29 NaiveDateTime::from_timestamp(1_483_228_799, 0), Utc); 30 let mut buf = Vec::with_capacity(100); 31 32 // formatting code from env_logger 33 const ITEMS: &[Item<'static>] = { 34 &[ 35 Numeric(Year, Zero), 36 Literal("-"), 37 Numeric(Month, Zero), 38 Literal("-"), 39 Numeric(Day, Zero), 40 Literal("T"), 41 Numeric(Hour, Zero), 42 Literal(":"), 43 Numeric(Minute, Zero), 44 Literal(":"), 45 Numeric(Second, Zero), 46 Fixed(TimezoneOffsetZ), 47 ] 48 }; 49 50 51 b.iter(|| { 52 buf.truncate(0); 53 write!(&mut buf, "{}", time.format_with_items(ITEMS.iter().cloned())) 54 .unwrap() 55 }); 56} 57