Debian Codegraph @

debian/rust-vendor/icu_segmenter/benches/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 use criterion::{Criterion, black_box, criterion_group, criterion_main};
6
7 use icu_segmenter::LineSegmenter;
8 use icu_segmenter::options::LineBreakOptions;
9 use icu_segmenter::options::LineBreakStrictness;
10 use icu_segmenter::options::LineBreakWordOption;
11
12 // Example is MIT license.
13 const TEST_STR_EN: &str = "Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.";
14 const TEST_STR_TH: &str =
15 "ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย ภาษาไทยภาษาไทย";
16
17 fn line_break_iter_latin1(c: &mut Criterion) {
18 let mut group = c.benchmark_group("Line Break/Latin1");
19
20 let segmenter = LineSegmenter::new_dictionary(Default::default());
21
22 let mut options = LineBreakOptions::default();
23 options.strictness = Some(LineBreakStrictness::Anywhere);
24 options.word_option = Some(LineBreakWordOption::BreakAll);
25 let segmenter_css = LineSegmenter::new_dictionary(options);
26
27 group.bench_function("En", |b| {
28 b.iter(|| {
29 black_box(&segmenter)
30 .segment_latin1(black_box(TEST_STR_EN).as_bytes())
31 .count()
32 })
33 });
34
35 group.bench_function("En CSS", |b| {
36 b.iter(|| {
37 black_box(&segmenter_css)
38 .segment_latin1(black_box(TEST_STR_EN).as_bytes())
39 .count()
40 })
41 });
42 }
43
44 fn line_break_iter_utf8(c: &mut Criterion) {
45 let mut group = c.benchmark_group("Line Break/UTF8");
46
47 let segmenter_auto = LineSegmenter::new_auto(Default::default());
48 let segmenter_lstm = LineSegmenter::new_lstm(Default::default());
49 let segmenter_dictionary = LineSegmenter::new_dictionary(Default::default());
50
51 let mut options = LineBreakOptions::default();
52 options.strictness = Some(LineBreakStrictness::Anywhere);
53 options.word_option = Some(LineBreakWordOption::BreakAll);
54 let segmenter_css_dictionary = LineSegmenter::new_dictionary(options);
55
56 // No need to test "auto", "lstm", or "dictionary" constructor variants since English uses only
57 // UAX14 rules for line breaking.
58 group.bench_function("En", |b| {
59 b.iter(|| {
60 black_box(&segmenter_dictionary)
61 .segment_str(black_box(TEST_STR_EN))
62 .count()
63 })
64 });
65
66 group.bench_function("En CSS", |b| {
67 b.iter(|| {
68 black_box(&segmenter_css_dictionary)
69 .segment_str(black_box(TEST_STR_EN))
70 .count()
71 })
72 });
73
74 let segmenters = [
75 (&segmenter_auto, "auto"),
76 (&segmenter_lstm, "lstm"),
77 (&segmenter_dictionary, "dictionary"),
78 ];
79 for (segmenter, variant) in segmenters {
80 group.bench_function("Th/".to_string() + variant, |b| {
81 b.iter(|| {
82 black_box(&segmenter)
83 .segment_str(black_box(TEST_STR_TH))
84 .count()
85 })
86 });
87 }
88 }
89
90 fn line_break_iter_utf16(c: &mut Criterion) {
91 let mut group = c.benchmark_group("Line Break/UTF16");
92
93 let utf16_en: Vec<u16> = TEST_STR_EN.encode_utf16().collect();
94 let utf16_th: Vec<u16> = TEST_STR_TH.encode_utf16().collect();
95
96 let segmenter_auto = LineSegmenter::new_auto(Default::default());
97 let segmenter_lstm = LineSegmenter::new_lstm(Default::default());
98 let segmenter_dictionary = LineSegmenter::new_dictionary(Default::default());
99
100 let mut options = LineBreakOptions::default();
101 options.strictness = Some(LineBreakStrictness::Anywhere);
102 options.word_option = Some(LineBreakWordOption::BreakAll);
103 let segmenter_css_dictionary = LineSegmenter::new_dictionary(options);
104
105 // No need to test "auto", "lstm", or "dictionary" constructor variants since English uses only
106 // UAX14 rules for line breaking.
107 group.bench_function("En", |b| {
108 b.iter(|| {
109 black_box(&segmenter_dictionary)
110 .segment_utf16(black_box(&utf16_en))
111 .count()
112 })
113 });
114
115 group.bench_function("En CSS", |b| {
116 b.iter(|| {
117 black_box(&segmenter_css_dictionary)
118 .segment_utf16(black_box(&utf16_en))
119 .count()
120 })
121 });
122
123 let segmenters = [
124 (&segmenter_auto, "auto"),
125 (&segmenter_lstm, "lstm"),
126 (&segmenter_dictionary, "dictionary"),
127 ];
128 for (segmenter, variant) in segmenters {
129 group.bench_function("Th/".to_string() + variant, |b| {
130 b.iter(|| {
131 black_box(&segmenter)
132 .segment_utf16(black_box(&utf16_th))
133 .count()
134 })
135 });
136 }
137 }
138
139 criterion_group!(
140 benches,
141 line_break_iter_latin1,
142 line_break_iter_utf8,
143 line_break_iter_utf16
144 );
145 criterion_main!(benches);