版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wowotuo/article/details/81813603
一、系统自带的包,time
有两种方式:都可以;缺点是,目前只有as_secs().
目前 as_millis() 还处于nightly阶段,后续才会推出。
use std::time::{Duration, SystemTime};
fn fib(x: i64) -> i64 {
match x <2{
true => x,
_ => fib(x - 2) + fib(x - 1),
}
}
fn main() {
let nums:Vec<i64> = vec![30_i64,35,40,45];
for n in nums {
let sy_time = SystemTime::now();
let value = fib(n);
println!("{:?},{:?}", SystemTime::now().duration_since(sy_time).unwrap().as_secs(),value);
println!("{:?},{:?}", sy_time.elapsed().unwrap().as_secs(),value);
}
thread::sleep_ms(500000);
}
二、外部的包,stopwatch=”0.0.7”
优点:精度高。
extern crate stopwatch;
use stopwatch::{Stopwatch};
fn fib(x: i64) -> i64 {
match x <2{
true => x,
_ => fib(x - 2) + fib(x - 1),
}
}
fn main() {
let nums:Vec<i64> = vec![30_i64,35,40,45];
for n in nums {
let sw = Stopwatch::start_new();
let value = fib(n);
println!("n:{0} recur value :{1} It took {2:.8} ms",n,value,sw.elapsed_ms());
}