Skip to content

Commit 199ebc7

Browse files
committed
rollup merge of rust-lang#19888: steveklabnik/gh19861
Fixes rust-lang#19861 /cc @huonw
2 parents 6f7faa0 + cd85f0a commit 199ebc7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/doc/guide-testing.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,8 @@ Advice on writing benchmarks:
503503
* Make the code in the `iter` loop do something simple, to assist in pinpointing
504504
performance improvements (or regressions)
505505

506+
## Gotcha: optimizations
507+
506508
There's another tricky part to writing benchmarks: benchmarks compiled with
507509
optimizations activated can be dramatically changed by the optimizer so that
508510
the benchmark is no longer benchmarking what one expects. For example, the
@@ -556,8 +558,12 @@ extern crate test;
556558
# struct X;
557559
# impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
558560
b.iter(|| {
559-
test::black_box(range(0u, 1000).fold(0, |old, new| old ^ new));
560-
});
561+
let mut n = 1000_u32;
562+
563+
test::black_box(&mut n); // pretend to modify `n`
564+
565+
range(0, n).fold(0, |a, b| a ^ b)
566+
})
561567
# }
562568
```
563569

@@ -573,3 +579,6 @@ test bench_xor_1000_ints ... bench: 1 ns/iter (+/- 0)
573579
574580
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
575581
```
582+
583+
However, the optimizer can still modify a testcase in an undesirable manner
584+
even when using either of the above.

0 commit comments

Comments
 (0)