我對Rust很新,我想要執行我的程序。我在網上搜索,但到目前爲止沒有找到。運行cargo build
後,我的代碼是這樣執行的:獲取執行時間與貨物
[email protected] ~/github/rust/hello_world [master *]
± % ./target/debug/hello_world
貨物是否有一個內置的方法的執行時間還是需要使用命令行?
我對Rust很新,我想要執行我的程序。我在網上搜索,但到目前爲止沒有找到。運行cargo build
後,我的代碼是這樣執行的:獲取執行時間與貨物
[email protected] ~/github/rust/hello_world [master *]
± % ./target/debug/hello_world
貨物是否有一個內置的方法的執行時間還是需要使用命令行?
您可以使用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 bench
running 1 test
test bench_xor_1000_ints ... bench: 131 ns/iter (+/- 3)
雖然這隻適用於夜間Rust。
貨物沒有固定時間的事物。您將需要使用您的操作系統機制。例如,在Linux或OS X,你也許可以使用time
:
time ./target/debug/hello_world
特別注意的是,你有一個調試版本。此沒有優化,不應該用於分析。相反,你應該在發佈模式生成代碼:
cargo build --release
,然後在target/release
目錄執行程序。
此外,您可能不希望包括貨物本身的時間。也就是說,做不是做到這些:
time cargo run
time cargo run --release