Benchmarking programs in Rust

2020-02-17 04:29发布

How is it possible to benchmark programs in Rust? For example, how would I get execution time of program in seconds?

8条回答
【Aperson】
2楼-- · 2020-02-17 05:30

I created a small crate for this (measure_time), which logs or prints the time until end of scope.

#[macro_use]
extern crate measure_time;
fn main() {
    print_time!("measure function");
    do_stuff();
}
查看更多
爷、活的狠高调
3楼-- · 2020-02-17 05:31

A quick way to find out the execution time of a program, regardless of implementation language, is to run time prog on the command line. For example:

~$ time sleep 4

real    0m4.002s
user    0m0.000s
sys     0m0.000s

The most interesting measurement is usually user, which measures the actual amount of work done by the program, regardless of what's going on in the system (sleep is a pretty boring program to benchmark). real measures the actual time that elapsed, and sys measures the amount of work done by the OS on behalf of the program.

查看更多
登录 后发表回答