Skip to content

Commit 3462b74

Browse files
committed
Improve readibility of 'final' specifier benchmark
1 parent 2ad3db8 commit 3462b74

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/final--benchmark.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff 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

2424
static 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
3333
BENCHMARK(WithoutFinal);
3434

3535
static 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
}

0 commit comments

Comments
 (0)