diff options
Diffstat (limited to 'src/counters/timer.rs')
| -rw-r--r-- | src/counters/timer.rs | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/src/counters/timer.rs b/src/counters/timer.rs index bc0397d..3ffef43 100644 --- a/src/counters/timer.rs +++ b/src/counters/timer.rs @@ -1,33 +1,36 @@ -use std::fmt::{Display, Error, Formatter}; +use std::{ + fmt::{Display, Error, Formatter}, + time::Duration, +}; /// A timer. #[derive(Copy, Clone, Debug, Default)] pub struct Timer { - time: f64, + time: Duration, #[allow(dead_code)] // The field isn’t used if the `profiler` feature isn’t enabled. - start: Option<f64>, + start: Option<std::time::Instant>, } impl Timer { /// Creates a new timer initialized to zero and not started. pub fn new() -> Self { Timer { - time: 0.0, + time: Duration::from_secs(0), start: None, } } /// Resets the timer to 0. pub fn reset(&mut self) { - self.time = 0.0 + self.time = Duration::from_secs(0) } /// Start the timer. pub fn start(&mut self) { #[cfg(feature = "profiler")] { - self.time = 0.0; - self.start = Some(instant::now()); + self.time = Duration::from_secs(0); + self.start = Some(web_time::Instant::now()); } } @@ -36,7 +39,7 @@ impl Timer { #[cfg(feature = "profiler")] { if let Some(start) = self.start { - self.time += instant::now() - start; + self.time += web_time::Instant::now().duration_since(start); } self.start = None; } @@ -46,18 +49,23 @@ impl Timer { pub fn resume(&mut self) { #[cfg(feature = "profiler")] { - self.start = Some(instant::now()); + self.start = Some(web_time::Instant::now()); } } /// The measured time between the last `.start()` and `.pause()` calls. - pub fn time(&self) -> f64 { + pub fn time(&self) -> Duration { self.time } + + /// The measured time in milliseconds between the last `.start()` and `.pause()` calls. + pub fn time_ms(&self) -> f64 { + self.time.as_secs_f64() * 1000.0 + } } impl Display for Timer { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { - write!(f, "{}s", self.time) + write!(f, "{}ms", self.time_ms()) } } |
