rustc_utils/
timer.rs

1//! A simple timer for profiling.
2
3use std::time::Instant;
4
5use log::info;
6
7pub fn elapsed(name: &str, start: Instant) {
8  info!("{name} took {:.04}s", start.elapsed().as_secs_f64());
9}
10
11pub struct BlockTimer<'a> {
12  pub name: &'a str,
13  pub start: Instant,
14}
15
16impl Drop for BlockTimer<'_> {
17  fn drop(&mut self) {
18    elapsed(self.name, self.start);
19  }
20}
21
22/// Logs the time taken from the start to the end of a syntactic block.
23#[macro_export]
24macro_rules! block_timer {
25  ($name:expr) => {
26    let name = $name;
27    let start = std::time::Instant::now();
28    let _timer = $crate::timer::BlockTimer { name, start };
29    log::info!("Starting {name}...");
30  };
31}