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