summaryrefslogtreecommitdiff
path: root/chomp-bench/benches/arith/main.rs
blob: 0e4e3f43b51707d1fec447a4c28cb47ce1bba830 (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
use chewed::{IterWrapper, Parser};
use chomp_bench::arith::*;
use criterion::{
    criterion_group, criterion_main, AxisScale, BenchmarkId, Criterion, PlotConfiguration,
    Throughput,
};

const INPUTS: &[&str] = &[
    include_str!("data/134.arith"),
    include_str!("data/262.arith"),
    include_str!("data/556.arith"),
    include_str!("data/1066.arith"),
    include_str!("data/2180.arith"),
    include_str!("data/4371.arith"),
    include_str!("data/8380.arith"),
    include_str!("data/17850.arith"),
    include_str!("data/35389.arith"),
];

fn parse_chewed(input: &str) -> i64 {
    IterWrapper::new(input.chars())
        .parse::<nibble::Ast>()
        .unwrap()
        .into()
}

fn parse_handwritten(input: &str) -> i64 {
    parse_expr(&mut IterWrapper::new(input.chars())).unwrap()
}

fn parse_lalrpop(parser: &lalr::ExpressionParser, input: &str) -> i64 {
    parser.parse(input).unwrap()
}

fn bench_parse(c: &mut Criterion) {
    let lalr_parser = lalr::ExpressionParser::new();
    let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
    let mut group = c.benchmark_group("Arith");
    group.plot_config(plot_config);
    for (i, input) in INPUTS.iter().enumerate() {
        group.throughput(Throughput::Bytes(input.len() as u64));
        group.bench_with_input(BenchmarkId::new("Chewed", i), *input, |b, i| {
            b.iter(|| parse_chewed(i))
        });
        group.bench_with_input(BenchmarkId::new("Handwritten", i), *input, |b, i| {
            b.iter(|| parse_handwritten(i))
        });
        group.bench_with_input(BenchmarkId::new("LALRPOP", i), *input, |b, i| {
            b.iter(|| parse_lalrpop(&lalr_parser, i))
        });
    }
}

criterion_group!(benches, bench_parse);
criterion_main!(benches);