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:
503
503
* Make the code in the ` iter ` loop do something simple, to assist in pinpointing
504
504
performance improvements (or regressions)
505
505
506
+ ## Gotcha: optimizations
507
+
506
508
There's another tricky part to writing benchmarks: benchmarks compiled with
507
509
optimizations activated can be dramatically changed by the optimizer so that
508
510
the benchmark is no longer benchmarking what one expects. For example, the
@@ -556,8 +558,12 @@ extern crate test;
556
558
# struct X ;
557
559
# impl X { fn iter <T , F >(& self , _ : F ) where F : FnMut () -> T {} } let b = X ;
558
560
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
+ })
561
567
# }
562
568
```
563
569
@@ -573,3 +579,6 @@ test bench_xor_1000_ints ... bench: 1 ns/iter (+/- 0)
573
579
574
580
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
575
581
```
582
+
583
+ However, the optimizer can still modify a testcase in an undesirable manner
584
+ even when using either of the above.
You can’t perform that action at this time.
0 commit comments