Giter Club home page Giter Club logo

microbench's Introduction

microbench

crates.io docs.rs Travis CI

A micro-benchmarking library (inspired by core_bench).

Released under the Apache License 2.0.

Supported on Rust 1.31.0 and later.

Note: The retain function (used to prevent the optimizer from removing computations) may not operate correctly or may have poor performance on the stable and beta channels of Rust. If you are using a nightly release of Rust, enable the nightly crate feature to enable a better implementation of this function.

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

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 fibonacci_recursive(n: u64) -> u64 {
    if n < 2 {
        n
    } else {
        fibonacci_recursive(n - 2) + fibonacci_recursive(n - 1)
    }
}

let options = Options::default();
microbench::bench(&options, "iterative_16", || fibonacci_iterative(16));
microbench::bench(&options, "recursive_16", || fibonacci_recursive(16));

Example output:

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.