diff options
author | tomsmeding <tom.smeding@gmail.com> | 2018-12-14 23:22:25 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2018-12-14 23:22:25 +0100 |
commit | b17507f88581672a8a1d30e624e57cd58caeeeb4 (patch) | |
tree | 8909af21fa06f5efacbfb3834827989a49e2b8ec /2018/benches | |
parent | 8a2f05141568fd3a5b29bb6e0623f4d96fd4d43b (diff) |
Try criterion for benchmarkingcriterion
Diffstat (limited to '2018/benches')
-rw-r--r-- | 2018/benches/day_benchmark.rs | 36 |
1 files changed, 36 insertions, 0 deletions
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<BufReader<File>> { + 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); |