-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathop_count_google_bench.hpp
More file actions
50 lines (48 loc) · 1.63 KB
/
op_count_google_bench.hpp
File metadata and controls
50 lines (48 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#include <benchmark/benchmark.h>
#ifndef BB_USE_OP_COUNT
namespace bb {
struct GoogleBenchOpCountReporter {
GoogleBenchOpCountReporter(::benchmark::State& state)
{
// unused, we don't have op counts on
(void)state;
}
};
}; // namespace bb
// require a semicolon to appease formatters
#define BB_REPORT_OP_COUNT_IN_BENCH(state) (void)0
#define BB_REPORT_OP_COUNT_BENCH_CANCEL() (void)0
#else
#include "op_count.hpp"
namespace bb {
// NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
struct GoogleBenchOpCountReporter {
// We allow having a ref member as this only lives inside a function frame
::benchmark::State& state;
bool cancelled = false;
GoogleBenchOpCountReporter(::benchmark::State& state)
: state(state)
{
// Intent: Clear when we enter the state loop
bb::detail::GLOBAL_OP_COUNTS.clear();
}
~GoogleBenchOpCountReporter()
{
// Allow for conditional reporting
if (cancelled) {
return;
}
// Intent: Collect results when we exit the state loop
for (auto& entry : bb::detail::GLOBAL_OP_COUNTS.get_aggregate_counts()) {
state.counters[entry.first] = static_cast<double>(entry.second);
}
}
};
// Allow for integration with google benchmark user-defined counters
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define BB_REPORT_OP_COUNT_IN_BENCH(state) GoogleBenchOpCountReporter __bb_report_op_count_in_bench{ state };
// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
#define BB_REPORT_OP_COUNT_BENCH_CANCEL() __bb_report_op_count_in_bench.cancelled = true;
}; // namespace bb
#endif