1 use criterion::{black_box, criterion_group, criterion_main, Criterion};
2 
3 use std::fs;
4 use unicode_segmentation::UnicodeSegmentation;
5 
word_bounds(c: &mut Criterion, lang: &str, path: &str)6 fn word_bounds(c: &mut Criterion, lang: &str, path: &str) {
7     let text = fs::read_to_string(path).unwrap();
8     c.bench_function(&format!("word_bounds_{}", lang), |bench| {
9         bench.iter(|| {
10             for w in text.split_word_bounds() {
11                 black_box(w);
12             }
13         });
14     });
15 }
16 
word_bounds_arabic(c: &mut Criterion)17 fn word_bounds_arabic(c: &mut Criterion) {
18     word_bounds(c, "arabic", "benches/texts/arabic.txt");
19 }
20 
word_bounds_english(c: &mut Criterion)21 fn word_bounds_english(c: &mut Criterion) {
22     word_bounds(c, "english", "benches/texts/english.txt");
23 }
24 
word_bounds_hindi(c: &mut Criterion)25 fn word_bounds_hindi(c: &mut Criterion) {
26     word_bounds(c, "hindi", "benches/texts/hindi.txt");
27 }
28 
word_bounds_japanese(c: &mut Criterion)29 fn word_bounds_japanese(c: &mut Criterion) {
30     word_bounds(c, "japanese", "benches/texts/japanese.txt");
31 }
32 
word_bounds_korean(c: &mut Criterion)33 fn word_bounds_korean(c: &mut Criterion) {
34     word_bounds(c, "korean", "benches/texts/korean.txt");
35 }
36 
word_bounds_mandarin(c: &mut Criterion)37 fn word_bounds_mandarin(c: &mut Criterion) {
38     word_bounds(c, "mandarin", "benches/texts/mandarin.txt");
39 }
40 
word_bounds_russian(c: &mut Criterion)41 fn word_bounds_russian(c: &mut Criterion) {
42     word_bounds(c, "russian", "benches/texts/russian.txt");
43 }
44 
word_bounds_source_code(c: &mut Criterion)45 fn word_bounds_source_code(c: &mut Criterion) {
46     word_bounds(c, "source_code", "benches/texts/source_code.txt");
47 }
48 
49 criterion_group!(
50     benches,
51     word_bounds_arabic,
52     word_bounds_english,
53     word_bounds_hindi,
54     word_bounds_japanese,
55     word_bounds_korean,
56     word_bounds_mandarin,
57     word_bounds_russian,
58     word_bounds_source_code,
59 );
60 
61 criterion_main!(benches);
62