Debian Codegraph @

third_party/rust/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-ca-buddhist",
16 "ca-ES-valencia",
17 "ca-ES-x-gbp",
18 "ca-ES-x-gbp-short",
19 "ca-ES-x-usd",
20 "ca-ES-xyzabc",
21 "ca-x-eur",
22 "cat",
23 "pl-Latn-PL",
24 "und",
25 "und-fonipa",
26 "und-u-ca-hebrew",
27 "und-u-ca-japanese",
28 "und-x-mxn",
29 "zh",
30 ];
31
32 fn overview_bench(c: &mut Criterion) {
33 c.bench_function("data_locale/overview", |b| {
34 b.iter(|| {
35 for s in black_box(BCP47_STRINGS).iter() {
36 let loc = DataLocale::from_str(s).unwrap();
37 let loc = loc.clone();
38 let s = loc.write_to_string();
39 loc.strict_cmp(s.as_bytes());
40 }
41 });
42 });
43
44 #[cfg(feature = "bench")]
45 data_locale_bench(c);
46 }
47
48 #[cfg(feature = "bench")]
49 fn data_locale_bench(c: &mut Criterion) {
50 c.bench_function("data_locale/parse", |b| {
51 b.iter(|| {
52 for s in black_box(BCP47_STRINGS).iter() {
53 DataLocale::from_str(s).unwrap();
54 }
55 });
56 });
57
58 let data_locales: Vec<DataLocale> = BCP47_STRINGS.iter().map(|s| s.parse().unwrap()).collect();
59
60 c.bench_function("data_locale/write_to_string", |b| {
61 b.iter(|| {
62 for loc in black_box(&data_locales).iter() {
63 loc.write_to_string();
64 }
65 });
66 });
67 c.bench_function("data_locale/clone", |b| {
68 b.iter(|| {
69 for loc in black_box(&data_locales).iter() {
70 let _ = loc.clone();
71 }
72 });
73 });
74 c.bench_function("data_locale/strict_cmp", |b| {
75 b.iter(|| {
76 for loc in black_box(&data_locales).iter() {
77 for s in black_box(BCP47_STRINGS).iter() {
78 loc.strict_cmp(s.as_bytes());
79 }
80 }
81 });
82 });
83 }
84
85 criterion_group!(benches, overview_bench,);
86 criterion_main!(benches);