jiff vs chrono benchmark comparing timestamp and civil arithmetic #235
Replies: 3 comments 18 replies
-
Can you provide a complete reproduction? e.g., Note though that you're using two very different duration types. Since you're using civil time here, the actual semantics here end up being the same, but a |
Beta Was this translation helpful? Give feedback.
-
OK, it took me a few minutes to go from your code to something I can actually run. It would definitely be appreciated to include a full reproduction in the future. But here's my [package]
publish = false
name = "jiff-v-chrono-benchmark"
version = "0.1.0"
edition = "2021"
[dependencies]
chrono = "0.4.39"
dateparser = "0.2.1"
divan = "0.1.17"
eyre = "0.6.12"
jiff = { path = "/home/andrew/rust/jiff/fixit" }
[[bench]]
name = "jiff-v-chrono"
path = "main.rs"
harness = false
[profile.release]
debug = true And my Rust source code, which I expanded quite a bit to include more benchmarks: use divan::{black_box, Bencher};
fn main() {
divan::main();
}
#[divan::bench]
fn test_jiff_civil_span(bencher: Bencher) {
use jiff::civil::DateTime;
use jiff::Span;
#[inline]
fn expand_interval_jiff(
begin: DateTime,
end: DateTime,
) -> eyre::Result<Vec<DateTime>> {
let mut d = begin;
let mut intv = vec![];
let day = Span::new().days(1);
while d < end {
intv.push(d);
d = d.checked_add(day).expect("overflow");
}
Ok(intv)
}
let begin = "2024-01-01".parse().unwrap();
let end = "2024-12-31".parse().unwrap();
bencher.bench_local(move || black_box(expand_interval_jiff(begin, end)));
}
#[divan::bench]
fn test_jiff_civil_duration(bencher: Bencher) {
use jiff::civil::DateTime;
use jiff::SignedDuration;
#[inline]
fn expand_interval_jiff(
begin: DateTime,
end: DateTime,
) -> eyre::Result<Vec<DateTime>> {
let mut d = begin;
let mut intv = vec![];
let day = SignedDuration::from_hours(24);
while d < end {
intv.push(d);
d = d.checked_add(day).expect("overflow");
}
Ok(intv)
}
let begin = "2024-01-01".parse().unwrap();
let end = "2024-12-31".parse().unwrap();
bencher.bench_local(move || black_box(expand_interval_jiff(begin, end)));
}
#[divan::bench]
fn test_jiff_timestamp_span(bencher: Bencher) {
use jiff::Span;
use jiff::Timestamp;
#[inline]
fn expand_interval_jiff(
begin: Timestamp,
end: Timestamp,
) -> eyre::Result<Vec<Timestamp>> {
let mut d = begin;
let mut intv = vec![];
let day = Span::new().hours(24);
while d < end {
intv.push(d);
d = d.checked_add(day).expect("overflow");
}
Ok(intv)
}
let begin = "2024-01-01T00:00Z".parse().unwrap();
let end = "2024-12-31T00:00Z".parse().unwrap();
bencher.bench_local(move || black_box(expand_interval_jiff(begin, end)));
}
#[divan::bench]
fn test_jiff_timestamp_duration(bencher: Bencher) {
use jiff::SignedDuration;
use jiff::Timestamp;
#[inline]
fn expand_interval_jiff(
begin: Timestamp,
end: Timestamp,
) -> eyre::Result<Vec<Timestamp>> {
let mut d = begin;
let mut intv = vec![];
let day = SignedDuration::from_hours(24);
while d < end {
intv.push(d);
d = d.checked_add(day).expect("overflow");
}
Ok(intv)
}
let begin = "2024-01-01T00:00Z".parse().unwrap();
let end = "2024-12-31T00:00Z".parse().unwrap();
bencher.bench_local(move || black_box(expand_interval_jiff(begin, end)));
}
#[divan::bench]
fn test_chrono_timestamp(bencher: Bencher) {
use chrono::{Duration, Utc};
#[inline]
fn expand_interval(
begin: chrono::DateTime<Utc>,
end: chrono::DateTime<Utc>,
) -> eyre::Result<Vec<chrono::DateTime<Utc>>> {
let mut d = begin;
let mut intv = vec![];
while d < end {
intv.push(d);
d += Duration::days(1);
}
Ok(intv)
}
let begin = dateparser::parse("2024-01-01").unwrap();
let end = dateparser::parse("2024-12-31").unwrap();
bencher.bench_local(move || black_box(expand_interval(begin, end)));
}
#[divan::bench]
fn test_chrono_civil(bencher: Bencher) {
use chrono::Duration;
#[inline]
fn expand_interval(
begin: chrono::NaiveDateTime,
end: chrono::NaiveDateTime,
) -> eyre::Result<Vec<chrono::NaiveDateTime>> {
let mut d = begin;
let mut intv = vec![];
while d < end {
intv.push(d);
d += Duration::days(1);
}
Ok(intv)
}
let begin = dateparser::parse("2024-01-01").unwrap().naive_utc();
let end = dateparser::parse("2024-12-31").unwrap().naive_utc();
bencher.bench_local(move || black_box(expand_interval(begin, end)));
} Where
I increased the sample iterations because I was getting some pretty massive noise otherwise. |
Beta Was this translation helpful? Give feedback.
-
And of course,
|
Beta Was this translation helpful? Give feedback.
-
Am I doing something wrong here, or is it expected?
gives the following results on an AMD 7700X 5.2 GHz.
Beta Was this translation helpful? Give feedback.
All reactions