I have a Rust program where I want to do some simple benchmarking with start time and end time!
use chrono::{NaiveTime, Utc};
fn main() {
let start_time: NaiveTime = Utc::now().time();
let end_time: NaiveTime = Utc::now().time();
println!("Total time taken to run is {}", end_time - start_time);
}
The code above prints as:
Total time taken to run is PT520.532696S
I guess it is 520 seconds if I'm not wrong, but how can I convert that into minutes? Is there a better way?
A simple look to the doc give the answer:
use chrono::Utc;
fn main() {
let start_time = Utc::now().time();
let end_time = Utc::now().time();
let diff = end_time - start_time;
println!("Total time taken to run is {}", diff.num_minutes());
}
but be aware that it's not a good way to mesure time in a monotonic way, this code could show -5 minutes if user change the system date somehow. Also call time()
remove the information of the date, and this is strange when you use chrono because generally you don't want to ignore the date so just remove time()
call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With