Benchmarking in nightly Rust
If you search online for information on how to benchmark in Rust, you will probably see a bunch of guides on how to do it in nightly Rust, but not many on how to do it in stable Rust. This is because the built-in Rust benchmarks are only available on the nightly channel. Let's start by explaining how the built-in benchmarks work, so that we can then find out how to do it in stable Rust.
First of all, let's see how to create benchmarks for a library. Imagine the following small library (code in lib.rs
):
//! This library gives a function to calculate Fibonacci numbers. /// Gives the Fibonacci sequence number for the given index. pub fn fibonacci(n: u32) -> u32 { if n == 0 || n == 1 { n } else { fibonacci(n - 1) + fibonacci(n - 2) } } /// Tests module. #[cfg(test)] mod tests { use super::*; /// Tests that the code gives the correct results. #[test] fn it_fibonacci() { assert_eq!(fibonacci(0), 0); assert_eq...