#[cfg(not(target_arch = "wasm32"))]
use std::time::{Duration, Instant};
#[cfg(not(target_arch = "wasm32"))]
#[cfg_attr(any(feature = "test", test), allow(dead_code))]
pub struct Timer {
timer: Instant,
}
#[cfg(not(target_arch = "wasm32"))]
impl Timer {
pub fn start() -> Self {
Self { timer: Instant::now() }
}
#[cfg_attr(any(feature = "test", test), allow(dead_code))]
pub fn elapsed_millis(&self) -> u64 {
Self::as_millis(self.timer.elapsed())
}
fn as_millis(duration: Duration) -> u64 {
duration.as_secs() * 1000 + u64::from(duration.subsec_millis())
}
}
#[cfg(all(target_arch = "wasm32", not(any(feature = "test", test))))]
extern "C" {
fn now() -> u32;
}
#[cfg(all(target_arch = "wasm32", not(any(feature = "test", test))))]
pub struct Timer {
start: u64,
}
#[cfg(all(target_arch = "wasm32", not(any(feature = "test", test))))]
impl Timer {
pub fn start() -> Self {
Self { start: Self::now() }
}
pub fn elapsed_millis(&self) -> u64 {
Self::now() - self.start
}
fn now() -> u64 {
(unsafe { now() }) as u64
}
}