Clarify the difference between `BENCHMARK_TEMPLATE_F` and `BENCHMARK_TEMPLATE_DEFINE_F` + `BENCHMARK_REGISTER_F` (#1815)

* Clarify BENCHMARK_REGISTER_F

Add comments highlighting the difference between `BENCHMARK_TEMPLATE_F` and `BENCHMARK_TEMPLATE_DEFINE_F`, mirroring those of `BENCHMARK_F ` and `BENCHMARK_DEFINE_F`.

* More informative comments.

* Update user_guide.md
This commit is contained in:
Jiawen (Kevin) Chen 2024-07-16 01:51:56 -07:00 committed by GitHub
parent 38df9daf48
commit d2cd246e19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 5 deletions

View File

@ -624,20 +624,22 @@ public:
} }
}; };
// Defines and registers `FooTest` using the class `MyFixture`.
BENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) { BENCHMARK_F(MyFixture, FooTest)(benchmark::State& st) {
for (auto _ : st) { for (auto _ : st) {
... ...
} }
} }
// Only defines `BarTest` using the class `MyFixture`.
BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) { BENCHMARK_DEFINE_F(MyFixture, BarTest)(benchmark::State& st) {
for (auto _ : st) { for (auto _ : st) {
... ...
} }
} }
/* BarTest is NOT registered */ // `BarTest` is NOT registered.
BENCHMARK_REGISTER_F(MyFixture, BarTest)->Threads(2); BENCHMARK_REGISTER_F(MyFixture, BarTest)->Threads(2);
/* BarTest is now registered */ // `BarTest` is now registered.
``` ```
### Templated Fixtures ### Templated Fixtures
@ -653,19 +655,22 @@ For example:
template<typename T> template<typename T>
class MyFixture : public benchmark::Fixture {}; class MyFixture : public benchmark::Fixture {};
// Defines and registers `IntTest` using the class template `MyFixture<int>`.
BENCHMARK_TEMPLATE_F(MyFixture, IntTest, int)(benchmark::State& st) { BENCHMARK_TEMPLATE_F(MyFixture, IntTest, int)(benchmark::State& st) {
for (auto _ : st) { for (auto _ : st) {
... ...
} }
} }
// Only defines `DoubleTest` using the class template `MyFixture<double>`.
BENCHMARK_TEMPLATE_DEFINE_F(MyFixture, DoubleTest, double)(benchmark::State& st) { BENCHMARK_TEMPLATE_DEFINE_F(MyFixture, DoubleTest, double)(benchmark::State& st) {
for (auto _ : st) { for (auto _ : st) {
... ...
} }
} }
// `DoubleTest` is NOT registered.
BENCHMARK_REGISTER_F(MyFixture, DoubleTest)->Threads(2); BENCHMARK_REGISTER_F(MyFixture, DoubleTest)->Threads(2);
// `DoubleTest` is now registered.
``` ```
<a name="custom-counters" /> <a name="custom-counters" />
@ -1012,11 +1017,11 @@ in any way. `<expr>` may even be removed entirely when the result is already
known. For example: known. For example:
```c++ ```c++
/* Example 1: `<expr>` is removed entirely. */ // Example 1: `<expr>` is removed entirely.
int foo(int x) { return x + 42; } int foo(int x) { return x + 42; }
while (...) DoNotOptimize(foo(0)); // Optimized to DoNotOptimize(42); while (...) DoNotOptimize(foo(0)); // Optimized to DoNotOptimize(42);
/* Example 2: Result of '<expr>' is only reused */ // Example 2: Result of '<expr>' is only reused.
int bar(int) __attribute__((const)); int bar(int) __attribute__((const));
while (...) DoNotOptimize(bar(0)); // Optimized to: while (...) DoNotOptimize(bar(0)); // Optimized to:
// int __result__ = bar(0); // int __result__ = bar(0);