#![allow(unused_doc_comments)]
#![allow(unused_imports)]
#![allow(dead_code)]
use std::time::Instant;
use std::time::Duration;
use std::sync::Mutex;
lazy_static::lazy_static! {
static ref STOPWATCH: Mutex<Option<STOPWATCH>> = Mutex::new(None);
}
struct Stopwatch {
start_time: Instant,
stop_time: Instant,
}
impl Stopwatch {
pub fn new() -> Stopwatch {
println!("Stopwatch Start...");
Stopwatch {
start_time: Instant::now(),
stop_time: Instant::now(),
}
}
pub fn start(&mut self) {
self.start_time = Instant::now();
self.stop_time = self.start_time;
}
pub fn stop(&mut self) {
self.stop_time = Instant::now();
let elapsed_time = self.duration();
println!("Stopwatch Stop... Elapsed time: {:?}", elapsed_time);
}
pub fn duration(&self) -> Duration {
self.stop_time - self.start_time
}
}
#[macro_export]
macro_rules! stopwatch_start {
() => {
let stopwatch = Stopwatch::new();
*STOPWATCH.lock().unwrap() = Some(stopwatch);
};
}
#[macro_export]
macro_rules! stopwatch_stop {
() => {
let mut stopwatch_option = STOPWATCH.lock().unwrap();
if let Some(ref mut stopwatch) = *stopwatch_option {
stopwatch.stop();
}
*stopwatch_option = None;
};
}
#[test]
fn test_stopwatch() {
use std::thread;
let sleeps = |second| {
let timeout = Duration::from_secs(second);
thread::park_timeout(timeout);
};
let mut stopwatch = Stopwatch::new();
stopwatch.start();
sleeps(1);
stopwatch.stop();
}