summaryrefslogtreecommitdiffstats
path: root/vendor/winnow-0.4.7/benches/number.rs
blob: b6c6fac5758d067b495e7dcbe19f7cb26cc57a8b (plain)
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#[macro_use]
extern crate criterion;

use criterion::Criterion;

use winnow::ascii::float;
use winnow::binary::be_u64;
use winnow::error::ErrMode;
use winnow::error::Error;
use winnow::error::ErrorKind;
use winnow::prelude::*;
use winnow::stream::ParseSlice;

type Stream<'i> = &'i [u8];

fn parser(i: Stream<'_>) -> IResult<Stream<'_>, u64> {
    be_u64(i)
}

fn number(c: &mut Criterion) {
    let data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];

    parser(&data[..]).expect("should parse correctly");
    c.bench_function("number", move |b| {
        b.iter(|| parser(&data[..]).unwrap());
    });
}

fn float_bytes(c: &mut Criterion) {
    println!(
        "float_bytes result: {:?}",
        float::<_, f64, Error<_>>(&b"-1.234E-12"[..])
    );
    c.bench_function("float bytes", |b| {
        b.iter(|| float::<_, f64, Error<_>>(&b"-1.234E-12"[..]));
    });
}

fn float_str(c: &mut Criterion) {
    println!(
        "float_str result: {:?}",
        float::<_, f64, Error<_>>("-1.234E-12")
    );
    c.bench_function("float str", |b| {
        b.iter(|| float::<_, f64, Error<_>>("-1.234E-12"));
    });
}

fn std_float(input: &[u8]) -> IResult<&[u8], f64, Error<&[u8]>> {
    match input.parse_slice() {
        Some(n) => Ok((&[], n)),
        None => Err(ErrMode::Backtrack(Error {
            input,
            kind: ErrorKind::Slice,
        })),
    }
}

fn std_float_bytes(c: &mut Criterion) {
    println!(
        "std_float_bytes result: {:?}",
        std_float(&b"-1.234E-12"[..])
    );
    c.bench_function("std_float bytes", |b| {
        b.iter(|| std_float(&b"-1.234E-12"[..]));
    });
}

criterion_group!(benches, number, float_bytes, std_float_bytes, float_str);
criterion_main!(benches);