From ec2651c3e4f56de8b1510df7dd2425e172d216ab Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Sun, 1 Dec 2024 21:32:17 -0800 Subject: [PATCH] boilerplate: improve duration printing --- boilerplate/src/main.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/boilerplate/src/main.rs b/boilerplate/src/main.rs index e6bd15f..790ac89 100644 --- a/boilerplate/src/main.rs +++ b/boilerplate/src/main.rs @@ -1,26 +1,28 @@ use std::fs::File; use std::io::{BufRead, BufReader, Lines}; -use std::time::Instant; +use std::time::{Duration, Instant}; // BOILERPLATE type InputIter = Lines>; -fn get_input() -> InputIter { - let f = File::open("input").unwrap(); - let br = BufReader::new(f); - br.lines() +fn duration_format(duration: &Duration) -> String { + match duration.as_secs_f64() { + x if x > 1.0 => format!("{:.3}s", x), + x if x > 0.010 => format!("{:.3}ms", x * 1e3), + x => format!("{:.3}us", x * 1e6), + } } fn main() { let start = Instant::now(); let ans1 = problem1(get_input()); let duration = start.elapsed(); - println!("Problem 1 solution: {} [{}s]", ans1, duration.as_secs_f64()); + println!("Problem 1 solution: {} [{}]", ans1, duration_format(&duration)); let start = Instant::now(); let ans2 = problem2(get_input()); let duration = start.elapsed(); - println!("Problem 2 solution: {} [{}s]", ans2, duration.as_secs_f64()); + println!("Problem 2 solution: {} [{}]", ans2, duration_format(&duration)); } // PROBLEM 1 solution