Debian Codegraph @

third_party/rust/winnow/benches/number.rs

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