From 20fa7cbfc0b5143f6c88e288772fc0f65610c2ec Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 15 Dec 2014 13:51:04 -0500 Subject: [PATCH 1/2] Add header to optimizations section --- src/doc/guide-testing.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/doc/guide-testing.md b/src/doc/guide-testing.md index 682c89fcc53..60ce71dac84 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 From cd85f0a56a790f24bb1c1928fbc37c9d24dd2937 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 15 Dec 2014 13:53:14 -0500 Subject: [PATCH 2/2] restore paragraph Fixes #19861 --- src/doc/guide-testing.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/doc/guide-testing.md b/src/doc/guide-testing.md index 60ce71dac84..8d113bb931a 100644 --- a/src/doc/guide-testing.md +++ b/src/doc/guide-testing.md @@ -556,8 +556,12 @@ extern crate test; # fn main() { # struct X; impl X { fn iter(&self, _: || -> 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 +577,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.