直接上代码
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
*/