1//! Time measurement.
2//!
34use std_::time::Duration;
56/// 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
10F: FnOnce() -> T,
11{
12let now = ::std_::time::Instant::now();
13let ret = f();
14let duration = now.elapsed();
15let microseconds = Duration::from(duration);
16 (microseconds, ret)
17}
1819/// 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
25F: FnOnce() -> Result<T, E>,
26{
27match measure(f) {
28 (_, Err(e)) => Err(e),
29 (t, Ok(v)) => Ok((t, v)),
30 }
31}
32