use std::time::Instant;
pub struct Stopwatch {
start: Instant,
}
impl Stopwatch {
pub fn since(&self) -> f64 {
self.start.elapsed().as_micros() as f64 / 1_000_000.0
}
}
impl Default for Stopwatch {
fn default() -> Self {
Self {
start: Instant::now(),
}
}
}
#[cfg(test)]
mod tests {
use std::thread::sleep;
use std::time::Duration;
use super::*;
#[test]
fn it_works() {
let stopwatch = Stopwatch::default();
sleep(Duration::from_millis(1));
assert!(stopwatch.since() > 0.0);
sleep(Duration::from_millis(100));
assert!(stopwatch.since() > 0.1);
sleep(Duration::from_millis(1000));
assert!(stopwatch.since() > 1.1);
}
}