Skip to content

Commit 4893114

Browse files
committed
rollup merge of rust-lang#19888: steveklabnik/gh19861
Fixes rust-lang#19861 /cc @huonw
2 parents 0be4630 + cd85f0a commit 4893114

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
@@ -554,8 +556,12 @@ extern crate test;
554556
# fn main() {
555557
# struct X; impl X { fn iter<T>(&self, _: || -> T) {} } let b = X;
556558
b.iter(|| {
557-
test::black_box(range(0u, 1000).fold(0, |old, new| old ^ new));
558-
});
559+
let mut n = 1000_u32;
560+
561+
test::black_box(&mut n); // pretend to modify `n`
562+
563+
range(0, n).fold(0, |a, b| a ^ b)
564+
})
559565
# }
560566
```
561567

@@ -571,3 +577,6 @@ test bench_xor_1000_ints ... bench: 1 ns/iter (+/- 0)
571577
572578
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
573579
```
580+
581+
However, the optimizer can still modify a testcase in an undesirable manner
582+
even when using either of the above.

0 commit comments

Comments
 (0)