rustc_utils/
timer.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
//! A simple timer for profiling.

use std::time::Instant;

use log::info;

pub fn elapsed(name: &str, start: Instant) {
  info!("{name} took {:.04}s", start.elapsed().as_secs_f64());
}

pub struct BlockTimer<'a> {
  pub name: &'a str,
  pub start: Instant,
}

impl Drop for BlockTimer<'_> {
  fn drop(&mut self) {
    elapsed(self.name, self.start);
  }
}

/// Logs the time taken from the start to the end of a syntactic block.
#[macro_export]
macro_rules! block_timer {
  ($name:expr) => {
    let name = $name;
    let start = std::time::Instant::now();
    let _timer = $crate::timer::BlockTimer { name, start };
    log::info!("Starting {name}...");
  };
}