about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-01-05 18:36:18 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-01-05 18:36:18 -0800
commit199ebc7bf7ea5c496df90109ad7dd60547537044 (patch)
tree7ee420a31f53a109e5d3e157ea1f4ab3c351d08a
parent6f7faa0b757a5afef33fcb9702f85a4a66341603 (diff)
parentcd85f0a56a790f24bb1c1928fbc37c9d24dd2937 (diff)
downloadrust-199ebc7bf7ea5c496df90109ad7dd60547537044.tar.gz
rust-199ebc7bf7ea5c496df90109ad7dd60547537044.zip
rollup merge of #19888: steveklabnik/gh19861
Fixes #19861

/cc @huonw
-rw-r--r--src/doc/guide-testing.md13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/doc/guide-testing.md b/src/doc/guide-testing.md
index 4606a1ba846..4c3d93bdfbe 100644
--- a/src/doc/guide-testing.md
+++ b/src/doc/guide-testing.md
@@ -503,6 +503,8 @@ Advice on writing benchmarks:
 * Make the code in the `iter` loop do something simple, to assist in pinpointing
   performance improvements (or regressions)
 
+## Gotcha: optimizations
+
 There's another tricky part to writing benchmarks: benchmarks compiled with
 optimizations activated can be dramatically changed by the optimizer so that
 the benchmark is no longer benchmarking what one expects. For example, the
@@ -556,8 +558,12 @@ extern crate test;
 # struct X;
 # impl X { fn iter<T, F>(&self, _: F) where F: FnMut() -> T {} } let b = X;
 b.iter(|| {
-    test::black_box(range(0u, 1000).fold(0, |old, new| old ^ new));
-});
+    let mut n = 1000_u32;
+
+    test::black_box(&mut n); // pretend to modify `n`
+
+    range(0, n).fold(0, |a, b| a ^ b)
+})
 # }
 ```
 
@@ -573,3 +579,6 @@ test bench_xor_1000_ints ... bench:       1 ns/iter (+/- 0)
 
 test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
 ```
+
+However, the optimizer can still modify a testcase in an undesirable manner
+even when using either of the above.