Debian Codegraph @

debian/missing-sources/icu_provider/benches/data_locale_bench.rs

raw
1 // This file is part of ICU4X. For terms of use, please see the file
2 // called LICENSE at the top level of the ICU4X source tree
3 // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5 extern crate alloc;
6
7 use criterion::{black_box, criterion_group, criterion_main, Criterion};
8 use icu_provider::prelude::*;
9 use std::str::FromStr;
10 use writeable::Writeable;
11
12 static BCP47_STRINGS: &[&str] = &[
13 "ca",
14 "ca-ES",
15 "ca-ES-u-sd-esar",
16 "ca-ES-valencia",
17 "ca-ES-xyzabc",
18 "cat",
19 "pl-Latn-PL",
20 "und",
21 "und-fonipa",
22 "zh",
23 ];
24
25 fn overview_bench(c: &mut Criterion) {
26 c.bench_function("data_locale/overview", |b| {
27 b.iter(|| {
28 for s in black_box(BCP47_STRINGS).iter() {
29 let loc = DataLocale::from_str(s).unwrap();
30 let s = loc.write_to_string();
31 loc.strict_cmp(s.as_bytes());
32 }
33 });
34 });
35
36 data_locale_bench(c);
37 }
38
39 fn data_locale_bench(c: &mut Criterion) {
40 c.bench_function("data_locale/parse", |b| {
41 b.iter(|| {
42 for s in black_box(BCP47_STRINGS).iter() {
43 DataLocale::from_str(s).unwrap();
44 }
45 });
46 });
47
48 let data_locales: Vec<DataLocale> = BCP47_STRINGS.iter().map(|s| s.parse().unwrap()).collect();
49
50 c.bench_function("data_locale/write_to_string", |b| {
51 b.iter(|| {
52 for loc in black_box(&data_locales).iter() {
53 loc.write_to_string();
54 }
55 });
56 });
57 c.bench_function("data_locale/strict_cmp", |b| {
58 b.iter(|| {
59 for loc in black_box(&data_locales).iter() {
60 for s in black_box(BCP47_STRINGS).iter() {
61 loc.strict_cmp(s.as_bytes());
62 }
63 }
64 });
65 });
66 }
67
68 criterion_group!(benches, overview_bench,);
69 criterion_main!(benches);