Skip to content

Commit 3a1e8b5

Browse files
committed
update criterion
1 parent d58a669 commit 3a1e8b5

2 files changed

Lines changed: 37 additions & 32 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ packed_simd = { version = "0.3.8", package = "packed_simd_2", optional = true }
2828
[dev-dependencies]
2929
quickcheck = "1.0"
3030
rand = "0.8"
31-
criterion = { version = "0.3", default-features = false }
31+
criterion = { version = "0.4", default-features = false }
3232

3333
[[bench]]
3434
name = "bench"

benches/bench.rs

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,86 @@
11
#[macro_use]
22
extern crate criterion;
3-
extern crate rand;
43
extern crate bytecount;
4+
extern crate rand;
55

6+
use criterion::{Bencher, BenchmarkId, Criterion};
7+
use rand::RngCore;
68
use std::env;
79
use std::time::Duration;
8-
use rand::RngCore;
9-
use criterion::{Bencher, Criterion, ParameterizedBenchmark};
1010

11-
use bytecount::{
12-
count, naive_count, naive_count_32,
13-
num_chars, naive_num_chars,
14-
};
11+
use bytecount::{count, naive_count, naive_count_32, naive_num_chars, num_chars};
1512

1613
fn random_bytes(len: usize) -> Vec<u8> {
1714
let mut result = vec![0; len];
1815
rand::thread_rng().fill_bytes(&mut result);
1916
result
2017
}
2118

22-
static COUNTS : &[usize] = &[0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
23-
100, 120, 140, 170, 210, 250, 300, 400, 500, 600, 700, 800, 900,
24-
1000, 1_000, 1_200, 1_400, 1_700, 2_100, 2_500, 3_000, 4_000,
25-
5_000, 6_000, 7_000, 8_000, 9_000, 10_000, 12_000, 14_000, 17_000,
26-
21_000, 25_000, 30_000, 100_000, 1_000_000];
19+
static COUNTS: &[usize] = &[
20+
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 140, 170, 210, 250, 300, 400, 500, 600, 700,
21+
800, 900, 1000, 1_000, 1_200, 1_400, 1_700, 2_100, 2_500, 3_000, 4_000, 5_000, 6_000, 7_000,
22+
8_000, 9_000, 10_000, 12_000, 14_000, 17_000, 21_000, 25_000, 30_000, 100_000, 1_000_000,
23+
];
2724

2825
fn get_counts() -> Vec<usize> {
29-
env::var("COUNTS").map(
30-
|s| s.split(',').map(
31-
|n| str::parse::<usize>(n).unwrap()).collect())
26+
env::var("COUNTS")
27+
.map(|s| {
28+
s.split(',')
29+
.map(|n| str::parse::<usize>(n).unwrap())
30+
.collect()
31+
})
3232
.unwrap_or(COUNTS.to_owned())
3333
}
3434

3535
fn get_config() -> Criterion {
3636
if env::var("CI").is_ok() {
37-
Criterion::default().nresamples(5_000)
38-
.without_plots()
39-
.measurement_time(Duration::new(2, 0))
40-
.warm_up_time(Duration::new(1, 0))
37+
Criterion::default()
38+
.nresamples(5_000)
39+
.without_plots()
40+
.measurement_time(Duration::new(2, 0))
41+
.warm_up_time(Duration::new(1, 0))
4142
} else {
4243
Criterion::default()
4344
}
4445
}
4546

4647
fn bench_counts(criterion: &mut Criterion) {
4748
fn naive(b: &mut Bencher, s: &usize) {
48-
let haystack = random_bytes(*s);
49+
let haystack = random_bytes(*s);
4950
b.iter(|| naive_count(&haystack, 10))
5051
}
5152
fn naive_32(b: &mut Bencher, s: &usize) {
52-
let haystack = random_bytes(*s);
53+
let haystack = random_bytes(*s);
5354
b.iter(|| naive_count_32(&haystack, 10))
5455
}
5556
fn hyper(b: &mut Bencher, s: &usize) {
56-
let haystack = random_bytes(*s);
57+
let haystack = random_bytes(*s);
5758
b.iter(|| count(&haystack, 10))
5859
}
5960
let counts = get_counts();
60-
criterion.bench("counts",
61-
ParameterizedBenchmark::new("naive", naive, counts)
62-
.with_function("naive_32", naive_32)
63-
.with_function("hyper", hyper));
61+
let mut group = criterion.benchmark_group("counts");
62+
for count in counts {
63+
group.bench_with_input(BenchmarkId::new("naive", count), &count, naive);
64+
group.bench_with_input(BenchmarkId::new("naive_32", count), &count, naive_32);
65+
group.bench_with_input(BenchmarkId::new("hyper", count), &count, hyper);
66+
}
6467
}
6568

6669
fn bench_num_chars(criterion: &mut Criterion) {
6770
fn naive(b: &mut Bencher, s: &usize) {
68-
let haystack = random_bytes(*s);
71+
let haystack = random_bytes(*s);
6972
b.iter(|| naive_num_chars(&haystack))
7073
}
7174
fn hyper(b: &mut Bencher, s: &usize) {
72-
let haystack = random_bytes(*s);
75+
let haystack = random_bytes(*s);
7376
b.iter(|| num_chars(&haystack))
7477
}
7578
let counts = get_counts();
76-
criterion.bench("num_chars",
77-
ParameterizedBenchmark::new("naive", naive, counts)
78-
.with_function("hyper", hyper));
79+
let mut group = criterion.benchmark_group("num_chars");
80+
for count in counts {
81+
group.bench_with_input(BenchmarkId::new("naive", count), &count, naive);
82+
group.bench_with_input(BenchmarkId::new("hyper", count), &count, hyper);
83+
}
7984
}
8085

8186
criterion_group!(name = count_bench; config = get_config(); targets = bench_counts);

0 commit comments

Comments
 (0)