Skip to content

Commit

Permalink
Add comparison to chrono_tz
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Dec 30, 2023
1 parent 114d6d2 commit 3e870be
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
3 changes: 2 additions & 1 deletion benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ license = "Apache-2.0"
publish = false

[dependencies]
chrono-tz = { version = "0.8", features = ["case-insensitive"] }
tzdb_data = { path = "../tzdb_data" }

[dev-dependencies]
criterion = { version = "0.5", default-features = false }
criterion = { version = "0.5", default-features = false, features = ["html_reports"] }
rand = { version = "0.8", default-features = false, features = ["std"] }
rand_xoshiro = "0.6"
test-strategy = "0.3"
Expand Down
57 changes: 55 additions & 2 deletions benchmarks/benches/by-name.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::convert::TryInto;
use std::str::FromStr;
use std::time::{Duration, Instant};

use criterion::black_box;
use rand::seq::{IteratorRandom, SliceRandom};
use rand::SeedableRng;
use rand_xoshiro::Xoroshiro128PlusPlus;
Expand Down Expand Up @@ -75,7 +77,9 @@ fn benchmark_by_name(c: &mut criterion::Criterion) {
let city = std::str::from_utf8(city).unwrap();

let raw_name = format!("{}/{}", continent, city);
let raw_len = crate::find_raw(raw_name.as_bytes()).unwrap_or_default().len();
let raw_len = crate::find_raw(raw_name.as_bytes())
.unwrap_or_default()
.len();
names.push((raw_name, raw_len));
}

Expand All @@ -91,7 +95,7 @@ fn benchmark_by_name(c: &mut criterion::Criterion) {
for &(ref name, raw_len) in names {
assert_eq!(
raw_len,
crate::find_raw(name.as_bytes()).unwrap_or_default().len(),
black_box(crate::find_raw(name.as_bytes())).unwrap_or_default().len(),
);
}
nanos += start.elapsed().as_nanos();
Expand All @@ -103,6 +107,54 @@ fn benchmark_by_name(c: &mut criterion::Criterion) {
)
});
});

// compare to chrono_tz
c.bench_function("chrono_tz::Tz::from_str", |b| {
b.iter_custom(|iters| {
let mut nanos = 0;
for i in 0..iters {
names.shuffle(&mut Xoroshiro128PlusPlus::seed_from_u64(i));

let start = Instant::now();
let names = criterion::black_box(&*names);
for &(ref name, _) in names {
if let Ok(tz) = chrono_tz::Tz::from_str(name) {
assert!(!black_box(tz.name()).is_empty());
}
}
nanos += start.elapsed().as_nanos();
}
Duration::from_nanos(
(nanos / names.len() as u128)
.try_into()
.expect("Did the test take 584 years to finish?"),
)
});
});

// compare to chrono_tz
c.bench_function("chrono_tz::Tz::from_str_insensitive", |b| {
b.iter_custom(|iters| {
let mut nanos = 0;
for i in 0..iters {
names.shuffle(&mut Xoroshiro128PlusPlus::seed_from_u64(i));

let start = Instant::now();
let names = criterion::black_box(&*names);
for &(ref name, _) in names {
if let Ok(tz) = chrono_tz::Tz::from_str_insensitive(name) {
assert!(!black_box(tz.name()).is_empty());
}
}
nanos += start.elapsed().as_nanos();
}
Duration::from_nanos(
(nanos / names.len() as u128)
.try_into()
.expect("Did the test take 584 years to finish?"),
)
});
});
}

fn main() {
Expand All @@ -113,6 +165,7 @@ fn main() {

criterion::Criterion::default()
.configure_from_args()
.with_plots()
.final_summary();
}
}

0 comments on commit 3e870be

Please sign in to comment.