Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 1008 Bytes

profile.zh.md

File metadata and controls

29 lines (22 loc) · 1008 Bytes

测量两个代码段间,已用的时间

[![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);
}