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
@@ -554,8 +556,12 @@ extern crate test;
554
556
# fn main () {
555
557
# struct X ; impl X { fn iter <T >(& self , _ : || -> T ) {} } let b = X ;
556
558
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
+ })
559
565
# }
560
566
```
561
567
@@ -571,3 +577,6 @@ test bench_xor_1000_ints ... bench: 1 ns/iter (+/- 0)
571
577
572
578
test result: ok. 0 passed; 0 failed; 0 ignored; 1 measured
573
579
```
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