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