Benchmarking your code
The Rust project has developed a testing crate for the compiler itself. Because it includes some quite useful features, most importantly a benchmarker, it is accessible on nightly builds as the built-in test
crate. Because it gets shipped with every nightly build, you don't need to add it to your Cargo.toml
to use it.
The test
crate is marked unstable because of its tight coupling to the compiler.
How to do it...
Open the
Cargo.toml
file that has been generated earlier for you.In the
bin
folder, create a file calledbenchmarking.rs
.Add the following code, and run it with
cargo bench
:
1 #![feature(test)] 2 // The test crate was primarily designed for 3 // the Rust compiler itself, so it has no stability guaranteed 4 extern crate test; 5 6 pub fn slow_fibonacci_recursive(n: u32) -> u32 { 7 match n { 8 0 => 0, 9 1 => 1, 10 _ => slow_fibonacci_recursive(n - 1) + slow_fibonacci_recursive(n - 2), 11 } 12 } 13 14 ...