core_extensions/measure_time/
mod.rs

1//! Time measurement.
2//!
3
4use std_::time::Duration;
5
6/// Measures the time taken by `f` to execute, returning a pair of `(Duration, T)`.
7#[inline(never)]
8pub fn measure<F, T>(f: F) -> (Duration, T)
9where
10    F: FnOnce() -> T,
11{
12    let now = ::std_::time::Instant::now();
13    let ret = f();
14    let duration = now.elapsed();
15    let microseconds = Duration::from(duration);
16    (microseconds, ret)
17}
18
19/// Measures the time taken by fallible function `f` to execute,
20/// returning a pair of `Result<(Duration, T), E>`,
21/// so that this function can be used in combination with `?`.
22#[inline(never)]
23pub fn try_measure<F, T, E>(f: F) -> Result<(Duration, T), E>
24where
25    F: FnOnce() -> Result<T, E>,
26{
27    match measure(f) {
28        (_, Err(e)) => Err(e),
29        (t, Ok(v)) => Ok((t, v)),
30    }
31}
32