diff --git a/bench/src/convert.rs b/bench/src/convert.rs index 442ff33..7755ed5 100644 --- a/bench/src/convert.rs +++ b/bench/src/convert.rs @@ -112,6 +112,13 @@ impl ConvertFrom for time::OffsetDateTime { } } +impl ConvertFrom for time::UtcDateTime { + fn convert_from(x: jiff::Timestamp) -> time::UtcDateTime { + time::UtcDateTime::from_unix_timestamp_nanos(x.as_nanosecond()) + .unwrap() + } +} + impl ConvertFrom for time::UtcOffset { fn convert_from(x: jiff::tz::Offset) -> time::UtcOffset { time::UtcOffset::from_whole_seconds(x.seconds()).unwrap() diff --git a/bench/src/timestamp.rs b/bench/src/timestamp.rs index 365092c..cc1c607 100644 --- a/bench/src/timestamp.rs +++ b/bench/src/timestamp.rs @@ -13,6 +13,7 @@ use crate::{benchmark, convert::ConvertFrom}; pub(super) fn define(c: &mut Criterion) { add_time_secs(c); add_time_subsec(c); + from_seconds(c); every_hour_in_week(c); to_civil_datetime_offset_conversion(c); to_civil_datetime_offset_holistic(c); @@ -118,6 +119,45 @@ fn add_time_subsec(c: &mut Criterion) { } } +/// Measures how long it takes to build the library's canonical "timestamp" +/// type from an actual integer number of seconds. +fn from_seconds(c: &mut Criterion) { + const NAME: &str = "timestamp/from_seconds"; + const SECONDS: i64 = 1719755160; + const EXPECTED: Timestamp = Timestamp::constant(SECONDS, 0); + + { + benchmark(c, format!("{NAME}/span/jiff"), |b| { + b.iter(|| { + let got = Timestamp::from_second(bb(SECONDS)).unwrap(); + assert_eq!(got, EXPECTED); + }) + }); + } + + { + let expected = chrono::DateTime::convert_from(EXPECTED); + benchmark(c, format!("{NAME}/duration/chrono"), |b| { + b.iter(|| { + let got = + chrono::DateTime::from_timestamp(bb(SECONDS), 0).unwrap(); + assert_eq!(got, expected); + }) + }); + } + + { + let expected = time::UtcDateTime::convert_from(EXPECTED); + benchmark(c, format!("{NAME}/duration/time"), |b| { + b.iter(|| { + let got = time::UtcDateTime::from_unix_timestamp(bb(SECONDS)) + .unwrap(); + assert_eq!(got, expected); + }) + }); + } +} + /// Measures the time it takes to iterate over a series of hours for 1 week. /// /// This is a regression benchmark for when the `Timestamp::series` API was