2015-11-05 47 views
0

我对Rust很新,我想要执行我的程序。我在网上搜索,但到目前为止没有找到。运行cargo build后,我的代码是这样执行的:获取执行时间与货物

[email protected] ~/github/rust/hello_world [master *] 
± % ./target/debug/hello_world 

货物是否有一个内置的方法的执行时间还是需要使用命令行?

回答

3

您可以使用time,这是一个命令行实用程序。

$ time ./target/debug/hello_world 
./target/debug/hello_world 3.02s user 0.18s system 34% cpu 9.177 total 

这将是最简单的方法。我不认为你可以直接通过cargo来标记编译步骤。

有类似的东西:cargo bench它允许你为你的程序编写基准。这给你非常具体的报告关于你的程序的某些部分的速度。

The docs have more reading

$ cargo bench 
running 1 test 
test bench_xor_1000_ints ... bench:  131 ns/iter (+/- 3) 

虽然这只适用于夜间Rust。

3

货物没有固定时间的事物。您将需要使用您的操作系统机制。例如,在Linux或OS X,你也许可以使用time

time ./target/debug/hello_world 

特别注意的是,你有一个调试版本。此没有优化,不应该用于分析。相反,你应该在发布模式生成代码:

cargo build --release 

,然后在target/release目录执行程序。

此外,您可能不希望包括货物本身的时间。也就是说,做不是做到这些:

time cargo run 
time cargo run --release