1 #[macro_use]
2 extern crate criterion;
3
4 use criterion::Criterion;
5
6 use winnow::ascii::float;
7 use winnow::binary::be_u64;
8 use winnow::error::ErrMode;
9 use winnow::error::ErrorKind;
10 use winnow::error::InputError;
11 use winnow::error::ParserError;
12 use winnow::prelude::*;
13 use winnow::stream::ParseSlice;
14
15 type Stream<'i> = &'i [u8];
16
parser(i: &mut Stream<'_>) -> PResult<u64>17 fn parser(i: &mut Stream<'_>) -> PResult<u64> {
18 be_u64.parse_next(i)
19 }
20
number(c: &mut Criterion)21 fn number(c: &mut Criterion) {
22 let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
23
24 parser
25 .parse_peek(&data[..])
26 .expect("should parse correctly");
27 c.bench_function("number", move |b| {
28 b.iter(|| parser.parse_peek(&data[..]).unwrap());
29 });
30 }
31
float_bytes(c: &mut Criterion)32 fn float_bytes(c: &mut Criterion) {
33 println!(
34 "float_bytes result: {:?}",
35 float::<_, f64, InputError<_>>.parse_peek(&b"-1.234E-12"[..])
36 );
37 c.bench_function("float bytes", |b| {
38 b.iter(|| float::<_, f64, InputError<_>>.parse_peek(&b"-1.234E-12"[..]));
39 });
40 }
41
float_str(c: &mut Criterion)42 fn float_str(c: &mut Criterion) {
43 println!(
44 "float_str result: {:?}",
45 float::<_, f64, InputError<_>>.parse_peek("-1.234E-12")
46 );
47 c.bench_function("float str", |b| {
48 b.iter(|| float::<_, f64, InputError<_>>.parse_peek("-1.234E-12"));
49 });
50 }
51
std_float(input: &mut &[u8]) -> PResult<f64>52 fn std_float(input: &mut &[u8]) -> PResult<f64> {
53 match input.parse_slice() {
54 Some(n) => Ok(n),
55 None => Err(ErrMode::from_error_kind(input, ErrorKind::Slice)),
56 }
57 }
58
std_float_bytes(c: &mut Criterion)59 fn std_float_bytes(c: &mut Criterion) {
60 println!(
61 "std_float_bytes result: {:?}",
62 std_float.parse_peek(&b"-1.234E-12"[..])
63 );
64 c.bench_function("std_float bytes", |b| {
65 b.iter(|| std_float.parse_peek(&b"-1.234E-12"[..]));
66 });
67 }
68
69 criterion_group!(benches, number, float_bytes, std_float_bytes, float_str);
70 criterion_main!(benches);
71