-
Open the Cargo.toml file that has been generated earlier for you.
-
In the bin folder, create a file called benchmarking.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 pub fn fibonacci_imperative(n: u32) -> u32 { 15 match n { 16 0 => 0, 17 1 => 1, 18 _ => { 19 let mut penultimate; 20 let mut last = 1; 21 let mut fib = 0; 22 for _ in 0..n { 23 penultimate = last; 24 last = fib; 25 fib = penultimate + last; 26 } 27 fib 28 } 29 } 30 } 31 32 pub fn memoized_fibonacci_recursive...