File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff 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+
506508There's another tricky part to writing benchmarks: benchmarks compiled with
507509optimizations activated can be dramatically changed by the optimizer so that
508510the 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 ;
556558b . 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
572578test 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.
You can’t perform that action at this time.
0 commit comments