From b17507f88581672a8a1d30e624e57cd58caeeeb4 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 14 Dec 2018 23:22:25 +0100 Subject: Try criterion for benchmarking --- 2018/benches/day_benchmark.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2018/benches/day_benchmark.rs (limited to '2018/benches/day_benchmark.rs') diff --git a/2018/benches/day_benchmark.rs b/2018/benches/day_benchmark.rs new file mode 100644 index 0000000..a37ba73 --- /dev/null +++ b/2018/benches/day_benchmark.rs @@ -0,0 +1,36 @@ +use std::io::{self, Read, BufReader, Error, ErrorKind}; +use std::fs::File; +use criterion::{Criterion, criterion_group, criterion_main}; + +// day 14: ~0.508 + +fn fibo(n: u64) -> u64 { + match n { + 0 => 1, + 1 => 1, + n => fibo(n-1) + fibo(n-2), + } +} + +fn fib_benchmark(c: &mut Criterion) { + c.bench_function("fib 20", |b| b.iter(|| fibo(20))); +} + +fn file_for_day(day: i32) -> io::Result> { + match File::open(format!("input/{}.txt", day)) { + Ok(f) => Ok(BufReader::new(f)), + Err(_) => Err(Error::new(ErrorKind::Other, format!("No input file for day {}", day))) + } +} + +fn day14_benchmark(c: &mut Criterion) { + let mut input = Vec::new(); + file_for_day(14).unwrap().read_to_end(&mut input).unwrap(); + + c.bench_function("day 14", |b| b.iter(|| + day14::main(BufReader::new(&input[..])).unwrap() + )); +} + +criterion_group!(benches, fib_benchmark); +criterion_main!(benches); -- cgit v1.2.3-70-g09d2