microbench

crates.io Travis CI

Documentation

A micro-benchmarking library.

Inspired by core_bench.

Released under the Apache License 2.0.

Overview

microbench uses linear regression to estimate the execution time of code segments. For example, the following table might represent data collected by microbench about a code segment.

| Iterations | Time (ns) | |------------|-----------| | 1 | 19 | | 2 | 25 | | 3 | 37 | | 4 | 47 | | 5 | 56 |

microbench of course takes many more than 5 samples and the number of iterations grows geometrically rather than linearly, but the idea remains the same. After collecting data like this, microbench uses ordinary least squares (OLS) linear regression to estimate the actual execution time of the code segment. Using OLS with the above data would yield an estimated execution time of 9.6 nanoseconds with a goodness of fit (R²) of 0.992.

Example

```rust use microbench::{self, Options};

fn fibonacci_iterative(n: u64) -> u64 { let (mut x, mut y, mut z) = (0, 1, 1); for _ in 0..n { x = y; y = z; z = x + y; } x }

fn fibonaccirecursive(n: u64) -> u64 { if n < 2 { n } else { fibonaccirecursive(n - 2) + fibonacci_recursive(n - 1) } }

let options = Options::default(); microbench::bench(&options, "iterative16", || fibonacciiterative(16)); microbench::bench(&options, "recursive16", || fibonaccirecursive(16)); ```

Example output:

console iterative_16 (5.0s) ... 281.733 ns/iter (0.998 R²) recursive_16 (5.0s) ... 9_407.020 ns/iter (0.997 R²)