File tree Expand file tree Collapse file tree 1 file changed +10
-10
lines changed
Expand file tree Collapse file tree 1 file changed +10
-10
lines changed Original file line number Diff line number Diff line change @@ -10,33 +10,33 @@ struct C : A {
1010 int f () override { return 1 ; }
1111};
1212
13- struct D : C {
14- int f () override { return 1 ; }
13+ struct D : C { // not used; for illustration only
14+ int f () override { return 1 ; } // does not change optimizer behavior
1515};
1616
17- int foo (B * b) { return b-> f (); }
17+ int foo (B & b) { return b. f (); }
1818
19- int bar (C * c) { return c-> f (); }
19+ int bar (C & c) { return c. f (); }
2020
2121// #region [Benchmark]
2222#include < benchmark/benchmark.h>
2323
2424static void WithoutFinal (benchmark::State &state) {
25- C *c = new C{};
26- int v = 0 ;
25+ C *c = new C{}; // better optimization if stack allocated;
26+ int v = 0 ; // here worst case
2727 for (auto _ : state) {
28- v += bar (c);
28+ v += bar (* c);
2929 }
3030 benchmark::DoNotOptimize (v);
3131}
3232// Register the function as a benchmark
3333BENCHMARK (WithoutFinal);
3434
3535static void WithFinal (benchmark::State &state) {
36- B *b = new B{};
37- int v = 0 ;
36+ B *b = new B{}; // better optimization if stack allocated;
37+ int v = 0 ; // here worst case
3838 for (auto _ : state) {
39- v += foo (b);
39+ v += foo (* b);
4040 }
4141 benchmark::DoNotOptimize (v);
4242}
You can’t perform that action at this time.
0 commit comments