Skip to content

Commit

Permalink
Add a benchmark
Browse files Browse the repository at this point in the history
See unicode-org/icu4x#4230 (review)

Results look like:
instanciate every time  time:   [9.5168 µs 9.5523 µs 9.5949 µs]
instanciate once        time:   [936.66 ns 942.79 ns 950.65 ns]
instanciate every time (typed)
                        time:   [9.1276 µs 9.1405 µs 9.1541 µs]
instanciate once (typed)
                        time:   [897.98 ns 916.17 ns 940.64 ns]
  • Loading branch information
g2p committed Oct 29, 2023
1 parent 39fa0a3 commit 39133b9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ intl-memoizer = "0.5"
unic-langid = "0.9"

[dev-dependencies]
criterion = "0.5"
fluent = "0.16"

[[bench]]
name = "icu-formatter-instanciation"
harness = false
57 changes: 57 additions & 0 deletions benches/icu-formatter-instanciation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use icu_calendar::{AnyCalendar, DateTime, Gregorian};
use icu_datetime::options::length;
use icu_datetime::{DateTimeFormatter, TypedDateTimeFormatter};
use icu_provider::DataLocale;

fn make_formatter(locale: &DataLocale) -> DateTimeFormatter {
// Using Time::Short otherwise try_new returns an Err(UnsupportedField(TimeZone(LowerZ)))
let length = length::Bag::from_date_time_style(length::Date::Full, length::Time::Short);
icu_datetime::DateTimeFormatter::try_new(locale, length.into()).unwrap()
}

fn format(formatter: &DateTimeFormatter, dt: &DateTime<AnyCalendar>) -> String {
formatter.format_to_string(dt).unwrap()
}

fn make_typed_formatter(locale: &DataLocale) -> TypedDateTimeFormatter<Gregorian> {
let length = length::Bag::from_date_time_style(length::Date::Full, length::Time::Short);
// Using Gregorian, not Iso, otherwise I get:
// the trait `CldrCalendar` is not implemented for `icu_calendar::Iso`
icu_datetime::TypedDateTimeFormatter::try_new(locale, length.into()).unwrap()
}

fn format_typed(formatter: &TypedDateTimeFormatter<Gregorian>, dt: &DateTime<Gregorian>) -> String {
formatter.format_to_string(dt)
}

pub fn criterion_benchmark(c: &mut Criterion) {
let locale = icu_locid::langid!("fr-FR");
let locale = &locale.into();
let datetime = DateTime::try_new_gregorian_datetime(1989, 11, 9, 23, 30, 0).unwrap();
let dt_any = datetime.clone().to_any();

c.bench_function("instanciate every time", |b| {
b.iter(|| format(&make_formatter(black_box(locale)), black_box(&dt_any)))
});
let formatter = make_formatter(locale);
c.bench_function("instanciate once", |b| {
b.iter(|| format(black_box(&formatter), black_box(&dt_any)))
});

c.bench_function("instanciate every time (typed)", |b| {
b.iter(|| {
format_typed(
&make_typed_formatter(black_box(locale)),
black_box(&datetime),
)
})
});
let formatter = make_typed_formatter(locale);
c.bench_function("instanciate once (typed)", |b| {
b.iter(|| format_typed(black_box(&formatter), black_box(&datetime)))
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit 39133b9

Please sign in to comment.