From 9d782fb50a25e3542497d89b8e78bdcb0722c537 Mon Sep 17 00:00:00 2001 From: Ummon Date: Fri, 6 Dec 2019 22:00:08 +0100 Subject: [PATCH] Better display of measured time --- src/main.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index b508728..56fefeb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,9 +27,19 @@ fn day03() -> String { ) } +fn format_micros(t: u128) -> String { + if t < 10_000 { + format!("{} μs", t) + } else if t < 10_000_000u128 { + format!("{} ms", t / 1_000u128) + } else { + format!("{} s", t / 1_000_000u128) + } +} + fn do_day(days: &[fn() -> String], day: usize) { let now = Instant::now(); - println!("Result of day {}: {} (time: {} μs)", day, days[day - 1](), now.elapsed().as_micros()); + println!("Result of day {}: {} (time: {})", day, days[day - 1](), format_micros(now.elapsed().as_micros())); } fn main() { -- 2.45.2