Use a macro for benchmarking.

With this macro the code needed for a new benchmark is minimal, but not
small enough IMO. Unfortunately, a better version would require
concat_ident, which is not going to happen soon.
This commit is contained in:
2018-12-18 15:54:11 +01:00
parent cc72c481cc
commit 477aaccc54

View File

@@ -2,48 +2,75 @@ extern crate aoc_2018;
#[macro_use]
extern crate bencher;
use std::fs::File;
use std::io::Read;
use bencher::Bencher;
use aoc_2018::get_impl;
const INPUTS: &[&[u8]] = &[
include_bytes!("../inputs/01.txt"),
include_bytes!("../inputs/02.txt"),
];
fn get_input(day: u32) -> Vec<u8> {
let filename = format!("inputs/{:02}.txt", day);
let mut buf = Vec::new();
let mut file = File::open(&filename).unwrap();
file.read_to_end(&mut buf).unwrap();
buf
}
fn test_part1(day: u32, bench: &mut Bencher) {
let input = get_input(day);
bench.iter(|| {
let input = INPUTS[day as usize - 1];
let mut instance = get_impl(day);
instance.part1(&mut input.as_ref())
instance.part1(&mut input.as_slice())
})
}
fn test_part2(day: u32, bench: &mut Bencher) {
let input = get_input(day);
bench.iter(|| {
let input = INPUTS[day as usize - 1];
let mut instance = get_impl(day);
instance.part2(&mut input.as_ref())
instance.part2(&mut input.as_slice())
})
}
fn day1_part1(bench: &mut Bencher) {
test_part1(1, bench)
macro_rules! day_bench {
( $name:ident, $day:expr ) => {
pub mod $name {
use super::*;
pub fn part1(bench: & mut Bencher) {
test_part1( $day, bench);
}
pub fn part2(bench: & mut Bencher) {
test_part2( $day, bench);
}
}
benchmark_group!( $name, $name::part1, $name::part2);
};
}
fn day1_part2(bench: &mut Bencher) {
test_part2(1, bench)
}
day_bench!(day01, 1);
day_bench!(day02, 2);
day_bench!(day03, 3);
day_bench!(day04, 4);
day_bench!(day05, 5);
day_bench!(day06, 6);
day_bench!(day07, 7);
day_bench!(day08, 8);
day_bench!(day09, 9);
day_bench!(day10, 10);
day_bench!(day11, 11);
day_bench!(day12, 12);
day_bench!(day13, 13);
day_bench!(day14, 14);
day_bench!(day15, 15);
day_bench!(day16, 16);
day_bench!(day17, 17);
fn day2_part1(bench: &mut Bencher) {
test_part1(2, bench)
}
fn day2_part2(bench: &mut Bencher) {
test_part2(2, bench)
}
benchmark_group!(day1, day1_part1, day1_part2);
benchmark_group!(day2, day2_part1, day2_part2);
benchmark_main!(day1, day2);
benchmark_main!(day01, day02, day03, day04, day05,
day06, day07, day08, day09, day10,
day11, day12, day13, day14, day15,
day16, day17);