Function tiny_bench::black_box

source ·
pub fn black_box<T>(dummy: T) -> T
Expand description

Everything in this module is more or less copied from criterion.rs with some rewrites to make it fit, the license is included in this file’s directory A function that is opaque to the optimizer, used to prevent the compiler from optimizing away computations in a benchmark.

This variant is stable-compatible, but it may cause some performance overhead or fail to prevent code from being eliminated.

use tiny_bench::black_box;
for i in 0..10 {
    assert_eq!(i, black_box(i));
}
Examples found in repository?
examples/bench_compare.rs (line 12)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn my_slow_function() {
    let mut num_iters = 0;
    for _ in 0..10_000 {
        num_iters += black_box(1);
    }
    assert_eq!(10_000, black_box(num_iters));
}

fn my_faster_function() {
    let mut num_iters = 0;
    for _ in 0..5_000 {
        num_iters += black_box(1);
    }
    assert_eq!(5_000, black_box(num_iters));
}