163 #ifndef BENCHMARK_BENCHMARK_H_
164 #define BENCHMARK_BENCHMARK_H_
167 #if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
168 #define BENCHMARK_HAS_CXX11
172 #if __cplusplus >= 201703L || \
173 (defined(_MSC_VER) && _MSC_VER >= 1911 && _MSVC_LANG >= 201703L)
174 #define BENCHMARK_HAS_CXX17
190 #include "benchmark/export.h"
192 #if defined(BENCHMARK_HAS_CXX11)
194 #include <initializer_list>
195 #include <type_traits>
199 #if defined(_MSC_VER)
203 #ifndef BENCHMARK_HAS_CXX11
204 #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
205 TypeName(const TypeName&); \
206 TypeName& operator=(const TypeName&)
208 #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
209 TypeName(const TypeName&) = delete; \
210 TypeName& operator=(const TypeName&) = delete
213 #ifdef BENCHMARK_HAS_CXX17
214 #define BENCHMARK_UNUSED [[maybe_unused]]
215 #elif defined(__GNUC__) || defined(__clang__)
216 #define BENCHMARK_UNUSED __attribute__((unused))
218 #define BENCHMARK_UNUSED
221 #if defined(__GNUC__) || defined(__clang__)
222 #define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline))
223 #define BENCHMARK_NOEXCEPT noexcept
224 #define BENCHMARK_NOEXCEPT_OP(x) noexcept(x)
225 #elif defined(_MSC_VER) && !defined(__clang__)
226 #define BENCHMARK_ALWAYS_INLINE __forceinline
228 #define BENCHMARK_NOEXCEPT noexcept
229 #define BENCHMARK_NOEXCEPT_OP(x) noexcept(x)
231 #define BENCHMARK_NOEXCEPT
232 #define BENCHMARK_NOEXCEPT_OP(x)
234 #define __func__ __FUNCTION__
236 #define BENCHMARK_ALWAYS_INLINE
237 #define BENCHMARK_NOEXCEPT
238 #define BENCHMARK_NOEXCEPT_OP(x)
241 #define BENCHMARK_INTERNAL_TOSTRING2(x) #x
242 #define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
245 #if defined(__GNUC__) || defined(__clang__)
246 #define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
247 #define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
248 #define BENCHMARK_DISABLE_DEPRECATED_WARNING \
249 _Pragma("GCC diagnostic push") \
250 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
251 #define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("GCC diagnostic pop")
253 #define BENCHMARK_BUILTIN_EXPECT(x, y) x
254 #define BENCHMARK_DEPRECATED_MSG(msg)
255 #define BENCHMARK_WARNING_MSG(msg) \
256 __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
257 __LINE__) ") : warning note: " msg))
258 #define BENCHMARK_DISABLE_DEPRECATED_WARNING
259 #define BENCHMARK_RESTORE_DEPRECATED_WARNING
263 #if defined(__GNUC__) && !defined(__clang__)
264 #define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
267 #ifndef __has_builtin
268 #define __has_builtin(x) 0
271 #if defined(__GNUC__) || __has_builtin(__builtin_unreachable)
272 #define BENCHMARK_UNREACHABLE() __builtin_unreachable()
273 #elif defined(_MSC_VER)
274 #define BENCHMARK_UNREACHABLE() __assume(false)
276 #define BENCHMARK_UNREACHABLE() ((void)0)
279 #ifdef BENCHMARK_HAS_CXX11
280 #define BENCHMARK_OVERRIDE override
282 #define BENCHMARK_OVERRIDE
285 #if defined(_MSC_VER)
286 #pragma warning(push)
288 #pragma warning(disable : 4251)
291 namespace benchmark {
292 class BenchmarkReporter;
294 BENCHMARK_EXPORT
void PrintDefaultHelp();
296 BENCHMARK_EXPORT
void Initialize(
int* argc,
char** argv,
297 void (*HelperPrinterf)() = PrintDefaultHelp);
298 BENCHMARK_EXPORT
void Shutdown();
302 BENCHMARK_EXPORT
bool ReportUnrecognizedArguments(
int argc,
char** argv);
305 BENCHMARK_EXPORT std::string GetBenchmarkFilter();
311 BENCHMARK_EXPORT
void SetBenchmarkFilter(std::string value);
314 BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
319 BENCHMARK_EXPORT BenchmarkReporter* CreateDefaultDisplayReporter();
337 BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks();
338 BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(std::string spec);
340 BENCHMARK_EXPORT
size_t
341 RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
342 BENCHMARK_EXPORT
size_t
343 RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::string spec);
345 BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(
346 BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter);
347 BENCHMARK_EXPORT
size_t
348 RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
349 BenchmarkReporter* file_reporter, std::string spec);
353 enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
355 BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
359 BENCHMARK_EXPORT
void SetDefaultTimeUnit(TimeUnit unit);
366 static const int64_t TombstoneValue;
372 total_allocated_bytes(TombstoneValue),
373 net_heap_growth(TombstoneValue) {}
379 int64_t max_bytes_used;
383 int64_t total_allocated_bytes;
388 int64_t net_heap_growth;
394 virtual void Start() = 0;
397 BENCHMARK_DEPRECATED_MSG(
"Use Stop(Result&) instead")
398 virtual
void Stop(Result* result) = 0;
401 BENCHMARK_DISABLE_DEPRECATED_WARNING
402 virtual
void Stop(Result& result) { Stop(&result); }
403 BENCHMARK_RESTORE_DEPRECATED_WARNING
409 void RegisterMemoryManager(MemoryManager* memory_manager);
413 void AddCustomContext(
const std::string& key,
const std::string& value);
418 class BenchmarkFamilies;
421 void UseCharPointer(
char const volatile*);
425 BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*);
428 BENCHMARK_EXPORT
int InitializeStreams();
429 BENCHMARK_UNUSED
static int stream_init_anchor = InitializeStreams();
433 #if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \
434 defined(__EMSCRIPTEN__)
435 #define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
440 #ifdef BENCHMARK_HAS_CXX11
441 inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
442 std::atomic_signal_fence(std::memory_order_acq_rel);
450 #ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
451 #if !defined(__GNUC__) || defined(__llvm__) || defined(__INTEL_COMPILER)
453 inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
454 asm volatile(
"" : :
"r,m"(value) :
"memory");
458 inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
459 #if defined(__clang__)
460 asm volatile(
"" :
"+r,m"(value) : :
"memory");
462 asm volatile(
"" :
"+m,r"(value) : :
"memory");
465 #elif defined(BENCHMARK_HAS_CXX11) && (__GNUC__ >= 5)
469 inline BENCHMARK_ALWAYS_INLINE
470 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
471 (
sizeof(Tp) <=
sizeof(Tp*))>::type
472 DoNotOptimize(Tp
const& value) {
473 asm volatile(
"" : :
"r,m"(value) :
"memory");
477 inline BENCHMARK_ALWAYS_INLINE
478 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
479 (
sizeof(Tp) >
sizeof(Tp*))>::type
480 DoNotOptimize(Tp
const& value) {
481 asm volatile(
"" : :
"m"(value) :
"memory");
485 inline BENCHMARK_ALWAYS_INLINE
486 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
487 (
sizeof(Tp) <=
sizeof(Tp*))>::type
488 DoNotOptimize(Tp& value) {
489 asm volatile(
"" :
"+m,r"(value) : :
"memory");
493 inline BENCHMARK_ALWAYS_INLINE
494 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
495 (
sizeof(Tp) >
sizeof(Tp*))>::type
496 DoNotOptimize(Tp& value) {
497 asm volatile(
"" :
"+m"(value) : :
"memory");
505 inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
506 asm volatile(
"" : :
"m"(value) :
"memory");
510 inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
511 asm volatile(
"" :
"+m"(value) : :
"memory");
515 #ifndef BENCHMARK_HAS_CXX11
516 inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
517 asm volatile(
"" : : :
"memory");
520 #elif defined(_MSC_VER)
522 inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
523 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
527 #ifndef BENCHMARK_HAS_CXX11
528 inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() { _ReadWriteBarrier(); }
532 inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
533 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
548 kAvgThreads = 1 << 1,
550 kAvgThreadsRate = kIsRate | kAvgThreads,
553 kIsIterationInvariant = 1 << 2,
557 kIsIterationInvariantRate = kIsRate | kIsIterationInvariant,
560 kAvgIterations = 1 << 3,
562 kAvgIterationsRate = kIsRate | kAvgIterations,
579 BENCHMARK_ALWAYS_INLINE
580 Counter(
double v = 0., Flags f = kDefaults, OneK k = kIs1000)
581 : value(v), flags(f), oneK(k) {}
583 BENCHMARK_ALWAYS_INLINE
operator double const &()
const {
return value; }
584 BENCHMARK_ALWAYS_INLINE
operator double&() {
return value; }
589 Counter::Flags
inline operator|(
const Counter::Flags& LHS,
590 const Counter::Flags& RHS) {
591 return static_cast<Counter::Flags
>(
static_cast<int>(LHS) |
592 static_cast<int>(RHS));
596 typedef std::map<std::string, Counter> UserCounters;
602 enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
604 typedef int64_t IterationCount;
606 enum StatisticUnit { kTime, kPercentage };
610 typedef double(BigOFunc)(IterationCount);
614 typedef double(StatisticsFunc)(
const std::vector<double>&);
619 StatisticsFunc* compute_;
622 Statistics(
const std::string& name, StatisticsFunc* compute,
623 StatisticUnit unit = kTime)
624 : name_(name), compute_(compute), unit_(unit) {}
632 enum AggregationReportMode
633 #if defined(BENCHMARK_HAS_CXX11)
642 ARM_Default = 1U << 0U,
644 ARM_FileReportAggregatesOnly = 1U << 1U,
646 ARM_DisplayReportAggregatesOnly = 1U << 2U,
648 ARM_ReportAggregatesOnly =
649 ARM_FileReportAggregatesOnly | ARM_DisplayReportAggregatesOnly
686 bool KeepRunningBatch(IterationCount n);
734 void SkipWithError(
const char* msg);
737 bool error_occurred()
const {
return error_occurred_; }
746 void SetIterationTime(
double seconds);
753 BENCHMARK_ALWAYS_INLINE
754 void SetBytesProcessed(int64_t bytes) {
755 counters[
"bytes_per_second"] =
756 Counter(
static_cast<double>(bytes), Counter::kIsRate, Counter::kIs1024);
759 BENCHMARK_ALWAYS_INLINE
760 int64_t bytes_processed()
const {
761 if (counters.find(
"bytes_per_second") != counters.end())
762 return static_cast<int64_t
>(counters.at(
"bytes_per_second"));
771 BENCHMARK_ALWAYS_INLINE
772 void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; }
774 BENCHMARK_ALWAYS_INLINE
775 int64_t complexity_length_n()
const {
return complexity_n_; }
783 BENCHMARK_ALWAYS_INLINE
784 void SetItemsProcessed(int64_t items) {
785 counters[
"items_per_second"] =
786 Counter(
static_cast<double>(items), benchmark::Counter::kIsRate);
789 BENCHMARK_ALWAYS_INLINE
790 int64_t items_processed()
const {
791 if (counters.find(
"items_per_second") != counters.end())
792 return static_cast<int64_t
>(counters.at(
"items_per_second"));
808 void SetLabel(
const char* label);
810 void BENCHMARK_ALWAYS_INLINE SetLabel(
const std::string& str) {
811 this->SetLabel(str.c_str());
815 BENCHMARK_ALWAYS_INLINE
816 int64_t range(std::size_t pos = 0)
const {
817 assert(range_.size() > pos);
821 BENCHMARK_DEPRECATED_MSG(
"use 'range(0)' instead")
822 int64_t range_x()
const {
return range(0); }
824 BENCHMARK_DEPRECATED_MSG(
"use 'range(1)' instead")
825 int64_t range_y()
const {
return range(1); }
828 BENCHMARK_ALWAYS_INLINE
829 int threads()
const {
return threads_; }
832 BENCHMARK_ALWAYS_INLINE
833 int thread_index()
const {
return thread_index_; }
835 BENCHMARK_ALWAYS_INLINE
836 IterationCount iterations()
const {
837 if (BENCHMARK_BUILTIN_EXPECT(!started_,
false)) {
840 return max_iterations - total_iterations_ + batch_leftover_;
847 IterationCount total_iterations_;
852 IterationCount batch_leftover_;
855 const IterationCount max_iterations;
860 bool error_occurred_;
863 std::vector<int64_t> range_;
865 int64_t complexity_n_;
869 UserCounters counters;
872 State(IterationCount max_iters,
const std::vector<int64_t>& ranges,
877 void StartKeepRunning();
880 bool KeepRunningInternal(IterationCount n,
bool is_batch);
881 void FinishKeepRunning();
883 const int thread_index_;
893 inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunning() {
894 return KeepRunningInternal(1,
false);
897 inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningBatch(IterationCount n) {
898 return KeepRunningInternal(n,
true);
901 inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningInternal(IterationCount n,
907 assert(is_batch || n == 1);
908 if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n,
true)) {
909 total_iterations_ -= n;
914 if (!error_occurred_ && total_iterations_ >= n) {
915 total_iterations_ -= n;
920 if (is_batch && total_iterations_ != 0) {
921 batch_leftover_ = n - total_iterations_;
922 total_iterations_ = 0;
931 typedef std::forward_iterator_tag iterator_category;
935 typedef std::ptrdiff_t difference_type;
939 BENCHMARK_ALWAYS_INLINE
942 BENCHMARK_ALWAYS_INLINE
944 : cached_(st->error_occurred_ ? 0 : st->max_iterations), parent_(st) {}
947 BENCHMARK_ALWAYS_INLINE
948 Value operator*()
const {
return Value(); }
950 BENCHMARK_ALWAYS_INLINE
951 StateIterator& operator++() {
957 BENCHMARK_ALWAYS_INLINE
958 bool operator!=(StateIterator
const&)
const {
959 if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0,
true))
return true;
960 parent_->FinishKeepRunning();
965 IterationCount cached_;
966 State*
const parent_;
969 inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
970 return StateIterator(
this);
972 inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
974 return StateIterator();
979 typedef void(Function)(State&);
995 Benchmark* Name(
const std::string& name);
1008 Benchmark* Range(int64_t start, int64_t limit);
1013 Benchmark* DenseRange(int64_t start, int64_t limit,
int step = 1);
1018 Benchmark* Args(
const std::vector<int64_t>& args);
1023 Benchmark* ArgPair(int64_t x, int64_t y) {
1024 std::vector<int64_t> args;
1033 Benchmark* Ranges(
const std::vector<std::pair<int64_t, int64_t> >& ranges);
1038 Benchmark* ArgsProduct(
const std::vector<std::vector<int64_t> >& arglists);
1041 Benchmark* ArgName(
const std::string& name);
1045 Benchmark* ArgNames(
const std::vector<std::string>& names);
1050 Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
1051 std::vector<std::pair<int64_t, int64_t> > ranges;
1052 ranges.push_back(std::make_pair(lo1, hi1));
1053 ranges.push_back(std::make_pair(lo2, hi2));
1054 return Ranges(ranges);
1081 Benchmark* RangeMultiplier(
int multiplier);
1100 Benchmark* Iterations(IterationCount n);
1111 Benchmark* ReportAggregatesOnly(
bool value =
true);
1114 Benchmark* DisplayAggregatesOnly(
bool value =
true);
1141 Benchmark* Complexity(BigO complexity = benchmark::oAuto);
1145 Benchmark* Complexity(BigOFunc* complexity);
1148 Benchmark* ComputeStatistics(
const std::string& name,
1149 StatisticsFunc* statistics,
1150 StatisticUnit unit = kTime);
1171 Benchmark* ThreadRange(
int min_threads,
int max_threads);
1177 Benchmark* DenseThreadRange(
int min_threads,
int max_threads,
int stride = 1);
1182 virtual void Run(
State& state) = 0;
1184 TimeUnit GetTimeUnit()
const;
1188 void SetName(
const char* name);
1190 int ArgsCnt()
const;
1197 AggregationReportMode aggregation_report_mode_;
1198 std::vector<std::string> arg_names_;
1199 std::vector<std::vector<int64_t> > args_;
1201 TimeUnit time_unit_;
1202 bool use_default_time_unit_;
1204 int range_multiplier_;
1206 double min_warmup_time_;
1207 IterationCount iterations_;
1209 bool measure_process_cpu_time_;
1210 bool use_real_time_;
1211 bool use_manual_time_;
1213 BigOFunc* complexity_lambda_;
1214 std::vector<Statistics> statistics_;
1215 std::vector<int> thread_counts_;
1218 callback_function setup_;
1219 callback_function teardown_;
1222 #if defined(BENCHMARK_HAS_CXX11)
1228 #if defined(BENCHMARK_HAS_CXX11)
1241 internal::Function* fn);
1243 #if defined(BENCHMARK_HAS_CXX11)
1244 template <
class Lambda>
1250 BENCHMARK_EXPORT
void ClearRegisteredBenchmarks();
1252 namespace internal {
1260 virtual void Run(
State& st) BENCHMARK_OVERRIDE;
1266 #ifdef BENCHMARK_HAS_CXX11
1267 template <
class Lambda>
1268 class LambdaBenchmark :
public Benchmark {
1270 virtual void Run(
State& st) BENCHMARK_OVERRIDE { lambda_(st); }
1273 template <
class OLambda>
1274 LambdaBenchmark(
const char* name, OLambda&& lam)
1275 : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
1277 LambdaBenchmark(LambdaBenchmark
const&) =
delete;
1279 template <
class Lam>
1280 friend Benchmark* ::benchmark::RegisterBenchmark(
const char*, Lam&&);
1287 inline internal::Benchmark* RegisterBenchmark(
const char* name,
1288 internal::Function* fn) {
1289 return internal::RegisterBenchmarkInternal(
1290 ::
new internal::FunctionBenchmark(name, fn));
1293 #ifdef BENCHMARK_HAS_CXX11
1294 template <
class Lambda>
1295 internal::Benchmark* RegisterBenchmark(
const char* name, Lambda&& fn) {
1297 internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
1298 return internal::RegisterBenchmarkInternal(
1299 ::
new BenchType(name, std::forward<Lambda>(fn)));
1303 #if defined(BENCHMARK_HAS_CXX11) && \
1304 (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
1305 template <
class Lambda,
class... Args>
1306 internal::Benchmark* RegisterBenchmark(
const char* name, Lambda&& fn,
1308 return benchmark::RegisterBenchmark(
1312 #define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
1320 virtual void Run(
State& st) BENCHMARK_OVERRIDE {
1322 this->BenchmarkCase(st);
1327 virtual void SetUp(
const State&) {}
1328 virtual void TearDown(
const State&) {}
1330 virtual void SetUp(
State& st) { SetUp(
const_cast<const State&
>(st)); }
1331 virtual void TearDown(
State& st) { TearDown(
const_cast<const State&
>(st)); }
1334 virtual void BenchmarkCase(
State&) = 0;
1344 #if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
1345 #define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
1347 #define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
1351 #ifdef BENCHMARK_HAS_CXX11
1352 #define BENCHMARK_PRIVATE_NAME(...) \
1353 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, \
1356 #define BENCHMARK_PRIVATE_NAME(n) \
1357 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
1360 #define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
1361 #define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
1363 #define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
1364 BaseClass##_##Method##_Benchmark
1366 #define BENCHMARK_PRIVATE_DECLARE(n) \
1367 static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
1370 #ifdef BENCHMARK_HAS_CXX11
1371 #define BENCHMARK(...) \
1372 BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1373 (::benchmark::internal::RegisterBenchmarkInternal( \
1374 new ::benchmark::internal::FunctionBenchmark(#__VA_ARGS__, \
1377 #define BENCHMARK(n) \
1378 BENCHMARK_PRIVATE_DECLARE(n) = \
1379 (::benchmark::internal::RegisterBenchmarkInternal( \
1380 new ::benchmark::internal::FunctionBenchmark(#n, n)))
1384 #define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
1385 #define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
1386 #define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
1387 #define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
1388 #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
1389 BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
1391 #ifdef BENCHMARK_HAS_CXX11
1404 #define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1405 BENCHMARK_PRIVATE_DECLARE(func) = \
1406 (::benchmark::internal::RegisterBenchmarkInternal( \
1407 new ::benchmark::internal::FunctionBenchmark( \
1408 #func "/" #test_case_name, \
1409 [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
1421 #define BENCHMARK_TEMPLATE1(n, a) \
1422 BENCHMARK_PRIVATE_DECLARE(n) = \
1423 (::benchmark::internal::RegisterBenchmarkInternal( \
1424 new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
1426 #define BENCHMARK_TEMPLATE2(n, a, b) \
1427 BENCHMARK_PRIVATE_DECLARE(n) = \
1428 (::benchmark::internal::RegisterBenchmarkInternal( \
1429 new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
1432 #ifdef BENCHMARK_HAS_CXX11
1433 #define BENCHMARK_TEMPLATE(n, ...) \
1434 BENCHMARK_PRIVATE_DECLARE(n) = \
1435 (::benchmark::internal::RegisterBenchmarkInternal( \
1436 new ::benchmark::internal::FunctionBenchmark( \
1437 #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1439 #define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
1442 #define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1443 class BaseClass##_##Method##_Benchmark : public BaseClass { \
1445 BaseClass##_##Method##_Benchmark() { \
1446 this->SetName(#BaseClass "/" #Method); \
1450 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1453 #define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1454 class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1456 BaseClass##_##Method##_Benchmark() { \
1457 this->SetName(#BaseClass "<" #a ">/" #Method); \
1461 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1464 #define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1465 class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
1467 BaseClass##_##Method##_Benchmark() { \
1468 this->SetName(#BaseClass "<" #a "," #b ">/" #Method); \
1472 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1475 #ifdef BENCHMARK_HAS_CXX11
1476 #define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
1477 class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1479 BaseClass##_##Method##_Benchmark() { \
1480 this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); \
1484 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1487 #define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) \
1488 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a)
1491 #define BENCHMARK_DEFINE_F(BaseClass, Method) \
1492 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1493 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1495 #define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \
1496 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1497 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1499 #define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \
1500 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1501 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1503 #ifdef BENCHMARK_HAS_CXX11
1504 #define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \
1505 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1506 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1508 #define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) \
1509 BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)
1512 #define BENCHMARK_REGISTER_F(BaseClass, Method) \
1513 BENCHMARK_PRIVATE_REGISTER_F(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method))
1515 #define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
1516 BENCHMARK_PRIVATE_DECLARE(TestName) = \
1517 (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
1520 #define BENCHMARK_F(BaseClass, Method) \
1521 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1522 BENCHMARK_REGISTER_F(BaseClass, Method); \
1523 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1525 #define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \
1526 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1527 BENCHMARK_REGISTER_F(BaseClass, Method); \
1528 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1530 #define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \
1531 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1532 BENCHMARK_REGISTER_F(BaseClass, Method); \
1533 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1535 #ifdef BENCHMARK_HAS_CXX11
1536 #define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \
1537 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1538 BENCHMARK_REGISTER_F(BaseClass, Method); \
1539 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1541 #define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) \
1542 BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)
1546 #define BENCHMARK_MAIN() \
1547 int main(int argc, char** argv) { \
1548 ::benchmark::Initialize(&argc, argv); \
1549 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
1550 ::benchmark::RunSpecifiedBenchmarks(); \
1551 ::benchmark::Shutdown(); \
1554 int main(int, char**)
1559 namespace benchmark {
1569 enum Scaling { UNKNOWN, ENABLED, DISABLED };
1573 double cycles_per_second;
1574 std::vector<CacheInfo> caches;
1575 std::vector<double> load_avg;
1581 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
CPUInfo);
1591 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
SystemInfo);
1598 std::string function_name;
1600 std::string min_time;
1601 std::string min_warmup_time;
1602 std::string iterations;
1603 std::string repetitions;
1604 std::string time_type;
1605 std::string threads;
1609 std::string str()
const;
1623 size_t name_field_width;
1624 static const char* executable_name;
1629 static const int64_t no_repetition_index = -1;
1630 enum RunType { RT_Iteration, RT_Aggregate };
1633 : run_type(RT_Iteration),
1634 aggregate_unit(kTime),
1635 error_occurred(
false),
1638 time_unit(GetDefaultTimeUnit()),
1639 real_accumulated_time(0),
1640 cpu_accumulated_time(0),
1641 max_heapbytes_used(0),
1643 complexity_lambda(),
1645 report_big_o(
false),
1647 memory_result(NULL),
1648 allocs_per_iter(0.0) {}
1650 std::string benchmark_name()
const;
1652 int64_t family_index;
1653 int64_t per_family_instance_index;
1655 std::string aggregate_name;
1656 StatisticUnit aggregate_unit;
1657 std::string report_label;
1658 bool error_occurred;
1659 std::string error_message;
1661 IterationCount iterations;
1663 int64_t repetition_index;
1664 int64_t repetitions;
1666 double real_accumulated_time;
1667 double cpu_accumulated_time;
1673 double GetAdjustedRealTime()
const;
1679 double GetAdjustedCPUTime()
const;
1682 double max_heapbytes_used;
1686 BigOFunc* complexity_lambda;
1687 int64_t complexity_n;
1690 const std::vector<internal::Statistics>* statistics;
1696 UserCounters counters;
1700 double allocs_per_iter;
1713 std::vector<BenchmarkReporter::Run> Runs;
1726 virtual bool ReportContext(
const Context& context) = 0;
1735 virtual void ReportRuns(
const std::vector<Run>& report) = 0;
1739 virtual void Finalize() {}
1743 void SetOutputStream(std::ostream* out) {
1745 output_stream_ = out;
1750 void SetErrorStream(std::ostream* err) {
1752 error_stream_ = err;
1755 std::ostream& GetOutputStream()
const {
return *output_stream_; }
1757 std::ostream& GetErrorStream()
const {
return *error_stream_; }
1759 virtual ~BenchmarkReporter();
1764 static void PrintBasicContext(std::ostream* out, Context
const& context);
1767 std::ostream* output_stream_;
1768 std::ostream* error_stream_;
1775 enum OutputOptions {
1779 OO_ColorTabular = OO_Color | OO_Tabular,
1780 OO_Defaults = OO_ColorTabular
1783 : output_options_(opts_), name_field_width_(0), printed_header_(
false) {}
1785 virtual bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1786 virtual void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1789 virtual void PrintRunData(
const Run& report);
1790 virtual void PrintHeader(
const Run& report);
1792 OutputOptions output_options_;
1793 size_t name_field_width_;
1794 UserCounters prev_counters_;
1795 bool printed_header_;
1801 virtual bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1802 virtual void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1803 virtual void Finalize() BENCHMARK_OVERRIDE;
1806 void PrintRunData(
const Run& report);
1811 class BENCHMARK_EXPORT BENCHMARK_DEPRECATED_MSG(
1812 "The CSV Reporter will be removed in a future release") CSVReporter
1815 CSVReporter() : printed_header_(false) {}
1816 virtual bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1817 virtual void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1820 void PrintRunData(
const Run& report);
1822 bool printed_header_;
1823 std::set<std::string> user_counter_names_;
1826 inline const char* GetTimeUnitString(TimeUnit unit) {
1837 BENCHMARK_UNREACHABLE();
1840 inline double GetTimeUnitMultiplier(TimeUnit unit) {
1851 BENCHMARK_UNREACHABLE();
1864 std::vector<int64_t> CreateRange(int64_t lo, int64_t hi,
int multi);
1868 std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit,
int step);
1872 #if defined(_MSC_VER)
1873 #pragma warning(pop)
Definition: benchmark.h:1617
Definition: benchmark.h:1773
Definition: benchmark.h:539
Definition: benchmark.h:1316
Definition: benchmark.h:1798
Definition: benchmark.h:364
Definition: benchmark.h:656
Definition: benchmark_register.cc:73
Definition: benchmark_api_internal.h:18
Definition: benchmark.h:987
Definition: benchmark.h:1255
Definition: perf_counters.h:134
Definition: thread_manager.h:12
Definition: thread_timer.h:10
Definition: benchmark.h:1597
Definition: benchmark.h:1619
Definition: benchmark.h:1703
Definition: benchmark.h:1628
Definition: benchmark.h:1562
Definition: benchmark.h:1561
Definition: benchmark.h:368
Definition: benchmark.h:930
Definition: benchmark.h:929
Definition: benchmark.h:1585
Definition: benchmark.h:617