fast_able/
elapsed_time.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::{fmt::Display, time::Duration};

// 记录耗时的功能
#[derive(Debug, Clone)]
pub struct ElapsedTime {
    pub start_time: std::time::Instant,
    elapsed_logs: Vec<(&'static str, Duration)>,
    total: Duration,
}

impl Display for ElapsedTime {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.print())
    }
}

impl ElapsedTime {
    pub fn new(capacity: usize) -> Self {
        Self {
            start_time: std::time::Instant::now(),
            elapsed_logs: Vec::with_capacity(capacity),
            total: Duration::ZERO,
        }
    }

    pub fn log(&mut self, log: &'static str) {
        let el = self.start_time.elapsed();
        let el2 = el - self.total;
        self.total = el;
        self.elapsed_logs.push((log, el2));
    }

    pub fn print_limit(&self, limit: Duration) -> Option<String> {
        if self.total < limit {
            return None;
        }
        Some(self.print())
    }

    pub fn print(&self) -> String {
        let logs = self
            .elapsed_logs
            .iter()
            .map(|(log, el)| format!("{log}: {el:?}"))
            .collect::<Vec<_>>()
            .join(", ");

        format!("Elapsed; total: {:?}, {logs}", self.total)
    }
}

#[test]
fn test_ElapsedTime() {
    let mut elapsed_time = ElapsedTime::new(10);
    for i in 0..10 {
        std::thread::sleep(std::time::Duration::from_millis(i * 100));
        elapsed_time.log("log");
    }
    println!("{}", elapsed_time.print());
}