• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::io::Write;
2 use std::time::Instant;
3 use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
4 
5 const ITERATIONS: u32 = 1000;
6 
time<T>(name: &'static str, function: impl Fn() -> T)7 pub fn time<T>(name: &'static str, function: impl Fn() -> T) {
8     let begin = Instant::now();
9     for _ in 0..ITERATIONS {
10         _ = function();
11     }
12     let micros = (begin.elapsed() / ITERATIONS).as_micros();
13     let mode = ["release", "debug"][cfg!(debug_assertions) as usize];
14     let mut writer = StandardStream::stderr(ColorChoice::Auto);
15     _ = writer.set_color(ColorSpec::new().set_fg(Some(Color::Magenta)));
16     _ = writeln!(&mut writer, "{} in {} mode: {} micros", name, mode, micros);
17 }
18