yaxpeax_core/
timing.rs

1use std::time::Instant;
2use std::collections::HashMap;
3
4pub struct Timer {
5    label: &'static str,
6    start: Instant
7}
8
9impl Timer {
10    pub fn start(label: &'static str) -> Self {
11        Self {
12            label,
13            start: Instant::now()
14        }
15    }
16
17    pub fn end(self) -> (&'static str, u64) {
18        (self.label, self.start.elapsed().as_nanos() as u64)
19    }
20}
21
22impl<A: yaxpeax_arch::Address> Default for Timings<A> {
23    fn default() -> Self {
24        Self::new()
25    }
26}
27
28#[derive(Serialize)]
29pub struct Timings<A: yaxpeax_arch::Address> {
30    // Timings for some label, measured as computed for address A, in `u64` nanoseconds.
31    data: HashMap<&'static str, Vec<(A, u64)>>,
32}
33
34impl<A: yaxpeax_arch::Address> Timings<A> {
35    pub fn new() -> Self {
36        Self { data: HashMap::new() }
37    }
38
39    pub fn record(&mut self, addr: A, timer: Timer) {
40        let (label, time) = timer.end();
41        let times = self.data.entry(label).or_insert_with(|| Vec::new());
42        times.push((addr, time));
43    }
44}