Closed

Description
The most common use of Duration
for me is benchmarking, and it's always a very unpleasant experience. This is what I typically write:
let now = Instant::now();
// Do something...
let elapsed = now.elapsed();
println!("{:?}", elapsed);
Output: Duration { secs: 1, nanos: 59219906 }
Wait, how much time has elapsed? That's terribly unreadable... :(
The docs for Duration
are not very helpful either. When you first see it, Duration
is pretty confusing, mostly because of the as_secs
/subsec_nanos
split.
Then I take a look at the docs for Instant
, but the examples show how to print the seconds part only.
Anyways, all I want to do is just print how much time has elapsed without squinting to connect secs
and nanos
together. Finally, I arrive at this horrible solution:
println!("seconds: {:.3}", elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1e9);
Three ways of fixing this come to my mind:
- Add a function to
Duration
that converts it to a (lossy)f64
format. - Add examples to the docs for
Instant
andDuration
that demonstrate how to print the duration time in a human readable form (e.g. total seconds with precision to 3 decimals). - Change the
Debug
formatting forDuration
and print it like this:Duration(secs.nanos: 1.059219906)
(note the leading zero). Or maybe evenDuration { secs: 1, nanos: 059219906 }
(again, the leading zero).
What do you think?