[![std-badge]][std] [![cat-time-badge]][cat-time]
测量time::Instant::elapsed
,从time::Instant::now
开始。
调用time::Instant::elapsed
,会返回一个time::Duration
,这个,我们将在示例末尾打印。此方法不会改变或重置time::Instant
对象。
use std::time::{Duration, Instant};
# use std::thread;
#
# fn expensive_function() {
# thread::sleep(Duration::from_secs(1));
# }
fn main() {
let start = Instant::now();
expensive_function();
let duration = start.elapsed();
println!("Time elapsed in expensive_function() is: {:?}", duration);
}