Rust高精度毫秒/微妙/納秒計時器

直接上代碼

use std::time::Instant; // timer

fn main() {
    let start = Instant::now();
 	
 	//even number range in[0, 4000001)
    let iter = (0..400_0001).filter(|x| x % 2 == 0);
    let res:i64 = iter.sum();
    println!("iter.sum is: {:?}", res);
    
    println!("time cost: {:?} ms", start.elapsed().as_millis());// ms
    println!("time cost: {:?} us", start.elapsed().as_micros());// us
    println!("time cost: {:?} ns", start.elapsed().as_nanos());// us
}

/*
iter.sum is: 4000002000000
time cost: 299 ms
time cost: 302236 us
time cost: 302829500 ns
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章