title: How can I prevent the Rust benchmark library from optimizing away my code?
body: <p>I have a simple idea I'm trying to benchmark in Rust. However, when I go to measure it using <code>test::Bencher</code>, the base case that I'm trying to compare against:</p>
  
  <pre><code>#![feature(test)]
  extern crate test;
  
  #[cfg(test)]
  mod tests {
  
      use test::black_box;
      use test::Bencher;
  
      const ITERATIONS: usize = 100_000;
  
      struct CompoundValue {
          pub a: u64,
          pub b: u64,
          pub c: u64,
          pub d: u64,
          pub e: u64,
      }
  
      #[bench]
      fn bench_in_place(b: &amp;mut Bencher) {
          let mut compound_value = CompoundValue {
              a: 0,
              b: 2,
              c: 0,
              d: 5,
              e: 0,
          };
  
          let val: &amp;mut CompoundValue = &amp;mut compound_value;
  
          let result = b.iter(|| {
              let mut f : u64 = black_box(0);
              for _ in 0..ITERATIONS {
                  f += val.a + val.b + val.c + val.d + val.e;
              }
              f = black_box(f);
              return f;
          });
          assert_eq!((), result);
      }
  }
  </code></pre>
  
  <p>is optimized away entirely by the compiler, resulting in:</p>
  
  <pre><code>running 1 test
  test tests::bench_in_place ... bench:           0 ns/iter (+/- 1)
  </code></pre>
  
  <p>As you can see in the gist, I have tried to employ the suggestions <a href="https://doc.rust-lang.org/book/benchmark-tests.html#gotcha:-optimizations" rel="nofollow">set forth in the documentation</a>, namely:</p>
  
  <ul>
  <li>Making use of the <code>test::black_box</code> method to hide implementation details from the compiler.</li>
  <li>Returning the calculated value from the closure passed to the <code>iter</code> method.</li>
  </ul>
  
  <p>Are there any other tricks I can try?</p>