use std::time::Instant;
pub struct Timer {
prefix: String,
start: Instant,
last: Instant,
}
impl Timer {
pub fn new<S: AsRef<str>>(prefix: S) -> Self {
let now = Instant::now();
Timer {
prefix: prefix.as_ref().to_string(),
start: now,
last: now,
}
}
pub fn delta<S: AsRef<str>>(&mut self, msg: S) -> f32 {
let now = Instant::now();
let duration_since_start = now.duration_since(self.start).as_secs_f32();
let duration_since_last = now.duration_since(self.last).as_secs_f32();
self.last = now;
println!(
"{}: {:.2?}/{:.2?}: {}",
self.prefix,
duration_since_last,
duration_since_start,
msg.as_ref(),
);
duration_since_last
}
}