Time 获得货物的执行时间

Time 获得货物的执行时间,time,command-line,rust,rust-cargo,Time,Command Line,Rust,Rust Cargo,我是个新手,我想给程序的执行计时。我在网上搜索,但到目前为止什么也没找到。运行cargo build后,我的代码执行如下: bling@bling ~/github/rust/hello_world [master *] ± % ./target/debug/hello_world Cargo是否有一种内置的方式来计时执行,或者我需要使用命令行?您可以使用time,它是一个命令行实用程序 $ time ./target/debug/hello_world ./target/debug/hell

我是个新手,我想给程序的执行计时。我在网上搜索,但到目前为止什么也没找到。运行
cargo build
后,我的代码执行如下:

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

Cargo是否有一种内置的方式来计时执行,或者我需要使用命令行?

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

$ time ./target/debug/hello_world
./target/debug/hello_world  3.02s user 0.18s system 34% cpu 9.177 total
这是最简单的方法。我认为您不能直接向
cargo
传递一个标志来计时编译步骤

还有类似的东西:
cargo bench
,它允许您为程序编写基准测试。这将为您提供有关程序某些部分的速度的非常具体的报告


虽然这只适用于夜间生锈。

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

time ./target/debug/hello_world
需要特别注意的是,您有一个调试构建。此没有优化,不应用于分析。相反,您应该在发布模式下构建代码:

cargo build --release
然后在
target/release
目录中执行程序

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

time cargo run
time cargo run --release

它并没有烘烤成货物,但hyperfine是用铁锈书写的,是时间的替代品:

cargo install hyperfine
hyperfine ./target/debug/hello_world
cargo install hyperfine
hyperfine ./target/debug/hello_world