df-helper 0.2.26

df helper tools db cache
Documentation
use std::time::Instant;
use std::time::Duration;

/// 计时器
pub struct Timer {
    start: Instant,
    unit: String,
}

/// 计时器
impl Timer {
    /// 计时开始
    /// unit 计时单位 秒 s 毫秒 ms 微秒 us
    pub fn start(unit: &str) -> Self {
        Self {
            start: Instant::now(),
            unit: unit.to_string(),
        }
    }
    /// 计时结束
    pub fn end(&mut self, print: bool) -> u128 {
        let duration = self.start.elapsed();
        let data = {
            match self.unit.as_str() {
                "ms" => {
                    let time = duration.as_millis();
                    time
                }
                "us" => {
                    let time = duration.as_micros();
                    time
                }
                _ => {
                    //s
                    let time = duration.as_secs();
                    time as u128
                }
            }
        };
        if print {
            println!("消耗时间: {} {}", data, self.unit);
        }
        return data;
    }
    /// 暂停时间
    /// 毫秒单位
    pub fn sleep(ms: u64) {
        let ten_millis = Duration::from_millis(ms);
        std::thread::sleep(ten_millis);
    }
}