boilerplate: improve duration printing

This commit is contained in:
Keenan Tims 2024-12-01 21:32:17 -08:00
parent 33c0c876c3
commit ec2651c3e4
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -1,26 +1,28 @@
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, Lines}; use std::io::{BufRead, BufReader, Lines};
use std::time::Instant; use std::time::{Duration, Instant};
// BOILERPLATE // BOILERPLATE
type InputIter = Lines<BufReader<File>>; type InputIter = Lines<BufReader<File>>;
fn get_input() -> InputIter { fn duration_format(duration: &Duration) -> String {
let f = File::open("input").unwrap(); match duration.as_secs_f64() {
let br = BufReader::new(f); x if x > 1.0 => format!("{:.3}s", x),
br.lines() x if x > 0.010 => format!("{:.3}ms", x * 1e3),
x => format!("{:.3}us", x * 1e6),
}
} }
fn main() { fn main() {
let start = Instant::now(); let start = Instant::now();
let ans1 = problem1(get_input()); let ans1 = problem1(get_input());
let duration = start.elapsed(); 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 start = Instant::now();
let ans2 = problem2(get_input()); let ans2 = problem2(get_input());
let duration = start.elapsed(); 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 // PROBLEM 1 solution