Skip to content

Commit 243af2c

Browse files
authored
Merge pull request #113 from indutny/fix/benchmarks
use criterion.rs for word benchmarks
2 parents 07e6155 + b59b1ce commit 243af2c

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

benches/unicode_words.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
1-
#[macro_use]
2-
extern crate bencher;
3-
extern crate unicode_segmentation;
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
42

5-
use bencher::Bencher;
63
use std::fs;
74
use unicode_segmentation::UnicodeSegmentation;
85

9-
fn unicode_words(bench: &mut Bencher, path: &str) {
6+
fn unicode_words(c: &mut Criterion, lang: &str, path: &str) {
107
let text = fs::read_to_string(path).unwrap();
11-
bench.iter(|| {
12-
for w in text.unicode_words() {
13-
bencher::black_box(w);
14-
}
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+
})
1514
});
16-
17-
bench.bytes = text.len() as u64;
1815
}
1916

20-
fn unicode_words_arabic(bench: &mut Bencher) {
21-
unicode_words(bench, "benches/texts/arabic.txt");
17+
fn unicode_words_arabic(c: &mut Criterion) {
18+
unicode_words(c, "arabic", "benches/texts/arabic.txt");
2219
}
2320

24-
fn unicode_words_english(bench: &mut Bencher) {
25-
unicode_words(bench, "benches/texts/english.txt");
21+
fn unicode_words_english(c: &mut Criterion) {
22+
unicode_words(c, "english", "benches/texts/english.txt");
2623
}
2724

28-
fn unicode_words_hindi(bench: &mut Bencher) {
29-
unicode_words(bench, "benches/texts/hindi.txt");
25+
fn unicode_words_hindi(c: &mut Criterion) {
26+
unicode_words(c, "hindi", "benches/texts/hindi.txt");
3027
}
3128

32-
fn unicode_words_japanese(bench: &mut Bencher) {
33-
unicode_words(bench, "benches/texts/japanese.txt");
29+
fn unicode_words_japanese(c: &mut Criterion) {
30+
unicode_words(c, "japanese", "benches/texts/japanese.txt");
3431
}
3532

36-
fn unicode_words_korean(bench: &mut Bencher) {
37-
unicode_words(bench, "benches/texts/korean.txt");
33+
fn unicode_words_korean(c: &mut Criterion) {
34+
unicode_words(c, "korean", "benches/texts/korean.txt");
3835
}
3936

40-
fn unicode_words_mandarin(bench: &mut Bencher) {
41-
unicode_words(bench, "benches/texts/mandarin.txt");
37+
fn unicode_words_mandarin(c: &mut Criterion) {
38+
unicode_words(c, "mandarin", "benches/texts/mandarin.txt");
4239
}
4340

44-
fn unicode_words_russian(bench: &mut Bencher) {
45-
unicode_words(bench, "benches/texts/russian.txt");
41+
fn unicode_words_russian(c: &mut Criterion) {
42+
unicode_words(c, "russian", "benches/texts/russian.txt");
4643
}
4744

48-
fn unicode_words_source_code(bench: &mut Bencher) {
49-
unicode_words(bench, "benches/texts/source_code.txt");
45+
fn unicode_words_source_code(c: &mut Criterion) {
46+
unicode_words(c, "source_code", "benches/texts/source_code.txt");
5047
}
5148

52-
benchmark_group!(
49+
criterion_group!(
5350
benches,
5451
unicode_words_arabic,
5552
unicode_words_english,
@@ -61,4 +58,4 @@ benchmark_group!(
6158
unicode_words_source_code,
6259
);
6360

64-
benchmark_main!(benches);
61+
criterion_main!(benches);

benches/word_bounds.rs

+26-29
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,52 @@
1-
#[macro_use]
2-
extern crate bencher;
3-
extern crate unicode_segmentation;
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
42

5-
use bencher::Bencher;
63
use std::fs;
74
use unicode_segmentation::UnicodeSegmentation;
85

9-
fn word_bounds(bench: &mut Bencher, path: &str) {
6+
fn word_bounds(c: &mut Criterion, lang: &str, path: &str) {
107
let text = fs::read_to_string(path).unwrap();
11-
bench.iter(|| {
12-
for w in text.split_word_bounds() {
13-
bencher::black_box(w);
14-
}
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+
});
1514
});
16-
17-
bench.bytes = text.len() as u64;
1815
}
1916

20-
fn word_bounds_arabic(bench: &mut Bencher) {
21-
word_bounds(bench, "benches/texts/arabic.txt");
17+
fn word_bounds_arabic(c: &mut Criterion) {
18+
word_bounds(c, "arabic", "benches/texts/arabic.txt");
2219
}
2320

24-
fn word_bounds_english(bench: &mut Bencher) {
25-
word_bounds(bench, "benches/texts/english.txt");
21+
fn word_bounds_english(c: &mut Criterion) {
22+
word_bounds(c, "english", "benches/texts/english.txt");
2623
}
2724

28-
fn word_bounds_hindi(bench: &mut Bencher) {
29-
word_bounds(bench, "benches/texts/hindi.txt");
25+
fn word_bounds_hindi(c: &mut Criterion) {
26+
word_bounds(c, "hindi", "benches/texts/hindi.txt");
3027
}
3128

32-
fn word_bounds_japanese(bench: &mut Bencher) {
33-
word_bounds(bench, "benches/texts/japanese.txt");
29+
fn word_bounds_japanese(c: &mut Criterion) {
30+
word_bounds(c, "japanese", "benches/texts/japanese.txt");
3431
}
3532

36-
fn word_bounds_korean(bench: &mut Bencher) {
37-
word_bounds(bench, "benches/texts/korean.txt");
33+
fn word_bounds_korean(c: &mut Criterion) {
34+
word_bounds(c, "korean", "benches/texts/korean.txt");
3835
}
3936

40-
fn word_bounds_mandarin(bench: &mut Bencher) {
41-
word_bounds(bench, "benches/texts/mandarin.txt");
37+
fn word_bounds_mandarin(c: &mut Criterion) {
38+
word_bounds(c, "mandarin", "benches/texts/mandarin.txt");
4239
}
4340

44-
fn word_bounds_russian(bench: &mut Bencher) {
45-
word_bounds(bench, "benches/texts/russian.txt");
41+
fn word_bounds_russian(c: &mut Criterion) {
42+
word_bounds(c, "russian", "benches/texts/russian.txt");
4643
}
4744

48-
fn word_bounds_source_code(bench: &mut Bencher) {
49-
word_bounds(bench, "benches/texts/source_code.txt");
45+
fn word_bounds_source_code(c: &mut Criterion) {
46+
word_bounds(c, "source_code", "benches/texts/source_code.txt");
5047
}
5148

52-
benchmark_group!(
49+
criterion_group!(
5350
benches,
5451
word_bounds_arabic,
5552
word_bounds_english,
@@ -61,4 +58,4 @@ benchmark_group!(
6158
word_bounds_source_code,
6259
);
6360

64-
benchmark_main!(benches);
61+
criterion_main!(benches);

0 commit comments

Comments
 (0)