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 (duration, ret)
16}
17
18/// Measures the time taken by fallible function `f` to execute,
19/// returning a pair of `Result<(Duration, T), E>`,
20/// so that this function can be used in combination with `?`.
21#[inline(never)]
22pub fn try_measure<F, T, E>(f: F) -> Result<(Duration, T), E>
23where
24 F: FnOnce() -> Result<T, E>,
25{
26 match measure(f) {
27 (_, Err(e)) => Err(e),
28 (t, Ok(v)) => Ok((t, v)),
29 }
30}
31