Simplest way to write filler statements that are not optimized away?

I'm benchmarking some threading code, whose code executed is just filler.

What's the simplest code I can use as filler, that is not optimized away in release mode?

As far as I can see, operations need to have a side effect (e.g. assignment to a variable), and if I'm assigning to a variable, the variable needs to be used.

Right now, I'm using something like:

let mut acc = 1_f64;

for ... {
  // blah
  acc += acc.sqrt();
}

if acc < 1.0 {
    println!("");
}

Is there any simpler way?

You need test::black_box from the nightly test crate.

And if gets optimized out, because you're trying to benchmark it, then just use #[bench] and Bencher to handle all of this properly for you, including reducing measurement noise.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.