8 releases

0.1.8 Jun 4, 2020
0.1.7 Jun 4, 2020
0.1.4 May 23, 2020

#314 in Profiling

MIT license

5KB

mybench

Crates.io

Simple (and very primitive) benchmarking macro.

Use cases:

  • bench!(wrapper, "Prompt") calculates average execution time for code inside wrapper function for 10,000 times.

  • bench!(wrapper, number_of_repetitions, "Prompt") calculates average execution time for code inside wrapper function for number_of_repetitions times.

Result is displayed as filename:row:col 'Prompt' xxx.yy ms

Examples


#[macro_use]
extern crate mybench;

#[test]
fn bench_ok() {
    // with wrapper function
    bench!(wrapper, "Prompt 1");
    bench!(wrapper, 100_000, "Prompt 2");

    // or you may use closure
    bench!(|| {
        for i in 0..1000 {
            let _ = i*i;
        }
    }, 
    100_000, "With closure");
}

fn wrapper() {
    for i in 0..1000 {
        let _ = i*i;
    }
}

Output:


running 1 test
tests\mybench.rs:6:5 'Prompt 1' 29.581µs
tests\mybench.rs:7:5 'Prompt 2' 29.693µs
test bench_ok ... ok

If you don't see stdout, try this: cargo test -- --show-output

If you see:


test bench_ok ... test bench_ok has been running for over 60 seconds

Don't worry, this is an intermediate result (the test is not yet completed). Wait a little longer and let the test end.

No runtime deps