summaryrefslogtreecommitdiffstats
path: root/third_party/rust/unic-langid-impl/benches/langid.rs
blob: 026c288602fb3d56a7b255b4e04b3b0686b46835 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use criterion::black_box;
use criterion::criterion_group;
use criterion::criterion_main;
use criterion::Criterion;
use criterion::Fun;

use unic_langid_impl::subtags;
use unic_langid_impl::LanguageIdentifier;

static STRINGS: &[&str] = &[
    "en-US",
    "en-GB",
    "es-AR",
    "it",
    "zh-Hans-CN",
    "de-AT",
    "pl",
    "fr-FR",
    "de-AT",
    "sr-Cyrl-SR",
    "nb-NO",
    "fr-FR",
    "mk",
    "uk",
    "en-US",
    "en-GB",
    "es-AR",
    "th",
    "de",
    "zh-Cyrl-HN",
    "en-Latn-US",
];

fn language_identifier_construct_bench(c: &mut Criterion) {
    let langids: Vec<LanguageIdentifier> = STRINGS
        .iter()
        .map(|s| -> LanguageIdentifier { s.parse().unwrap() })
        .collect();

    let funcs = vec![
        Fun::new("from_str", |b, _| {
            b.iter(|| {
                for s in STRINGS {
                    let _: Result<LanguageIdentifier, _> = black_box(s).parse();
                }
            })
        }),
        Fun::new("from_bytes", |b, _| {
            let slices: Vec<&[u8]> = STRINGS.iter().map(|s| s.as_bytes()).collect();
            b.iter(|| {
                for s in &slices {
                    let _ = LanguageIdentifier::from_bytes(black_box(s));
                }
            })
        }),
        Fun::new("from_parts", |b, langids: &Vec<LanguageIdentifier>| {
            let entries: Vec<(
                subtags::Language,
                Option<subtags::Script>,
                Option<subtags::Region>,
                Vec<subtags::Variant>,
            )> = langids
                .iter()
                .cloned()
                .map(|langid| langid.into_parts())
                .collect();
            b.iter(|| {
                for (language, script, region, variants) in &entries {
                    let _ = LanguageIdentifier::from_parts(
                        language.clone(),
                        script.clone(),
                        region.clone(),
                        variants,
                    );
                }
            })
        }),
    ];

    c.bench_functions("language_identifier_construct", funcs, langids);
}

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