Skip to content

Commit c07d51b

Browse files
committed
Simplify the logic for converting elapsed time to appropriate units
1 parent 2cd77d4 commit c07d51b

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

src/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -454,21 +454,18 @@ where
454454
}
455455

456456
fn format_timestamp_with_decimals(&self, start: std::time::Instant) -> String {
457-
let elapsed = start.elapsed();
458-
let nanos = elapsed.as_nanos() as f64;
459-
let micros = elapsed.as_micros() as f64;
460-
let millis = elapsed.as_millis() as f64;
457+
let secs = start.elapsed().as_secs_f64();
461458

462459
// Convert elapsed time to appropriate units: μs, ms, or s.
463460
// - Less than 1ms: use μs
464461
// - Less than 1s : use ms
465462
// - 1s and above : use s
466-
let (n, unit) = if micros < 1000.0 {
467-
(nanos / 1000.0, "μs")
468-
} else if millis < 1000.0 {
469-
(micros / 1000.0, "ms")
463+
let (n, unit) = if secs < 0.001 {
464+
(secs * 1_000_000.0, "μs")
465+
} else if secs < 1.0 {
466+
(secs * 1_000.0, "ms")
470467
} else {
471-
(millis / 1000.0, "s ")
468+
(secs, "s ")
472469
};
473470

474471
let n = format!(" {n:.2}");

0 commit comments

Comments
 (0)