summaryrefslogtreecommitdiff
path: root/2018/benches/day_benchmark.rs
diff options
context:
space:
mode:
Diffstat (limited to '2018/benches/day_benchmark.rs')
-rw-r--r--2018/benches/day_benchmark.rs36
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);