通过Cargo获得执行时间

时间:2015-11-05 20:59:26

标签: time command-line rust rust-cargo

我是Rust的新手,我想为我的程序执行时间。我在网上搜索,但到目前为止一无所获。运行cargo build后,我的代码执行如下:

bling@bling ~/github/rust/hello_world [master *]
± % ./target/debug/hello_world

Cargo是否有内置的方法来执行时间,还是需要使用命令行?

3 个答案:

答案 0 :(得分: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。

答案 1 :(得分:3)

货物没有内置的计时方式。您将需要使用您的操作系统机制。例如,在Linux或OS X上,您可以使用time

time ./target/debug/hello_world

特别值得注意的是,您有调试版本。此没有优化,不应用于分析。相反,您应该在发布模式下构建代码:

cargo build --release

然后执行target/release目录中的程序。

此外,您可能不希望包含货物本身的时间。也就是说,执行以下任一操作:

time cargo run
time cargo run --release

答案 2 :(得分:0)

它没有被烘烤成货物,但是超细粉写成了锈迹,可以代替时间:

<ng5-slider (valueChange)="logValue($event)" [(value)]='value' [(highValue)]='highValue' [options]='options'>