1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
use criterion::black_box;
use winnow::combinator::repeat;
use winnow::prelude::*;
use winnow::token::take_until0;
fn find_slice(c: &mut criterion::Criterion) {
let empty = "";
let start_byte = "\r".repeat(100);
let start_slice = "\r\n".repeat(100);
let small = format!("{:>10}\r\n", "").repeat(100);
let large = format!("{:>10000}\r\n", "").repeat(100);
let data = [
("empty", (empty, empty)),
("start", (&start_byte, &start_slice)),
("medium", (&small, &small)),
("large", (&large, &large)),
];
let mut group = c.benchmark_group("find_slice");
for (name, samples) in data {
group.bench_with_input(
criterion::BenchmarkId::new("byte", name),
samples.0,
|b, sample| {
b.iter(|| black_box(parser_byte.parse_peek(black_box(sample)).unwrap()));
},
);
group.bench_with_input(
criterion::BenchmarkId::new("slice", name),
samples.1,
|b, sample| {
b.iter(|| black_box(parser_slice.parse_peek(black_box(sample)).unwrap()));
},
);
}
group.finish();
}
fn parser_byte(input: &mut &str) -> PResult<usize> {
repeat(0.., (take_until0("\r"), "\r")).parse_next(input)
}
fn parser_slice(input: &mut &str) -> PResult<usize> {
repeat(0.., (take_until0("\r\n"), "\r\n")).parse_next(input)
}
criterion::criterion_group!(benches, find_slice);
criterion::criterion_main!(benches);
|