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>
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#elif defined(_MSC_VER) && !defined(__clang__)
224#define BENCHMARK_ALWAYS_INLINE __forceinline
225#define __func__ __FUNCTION__
227#define BENCHMARK_ALWAYS_INLINE
230#define BENCHMARK_INTERNAL_TOSTRING2(x) #x
231#define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
234#if defined(__GNUC__) && !defined(__NVCC__) || defined(__clang__)
235#define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
236#define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
237#define BENCHMARK_DISABLE_DEPRECATED_WARNING \
238 _Pragma("GCC diagnostic push") \
239 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
240#define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("GCC diagnostic pop")
242#define BENCHMARK_BUILTIN_EXPECT(x, y) x
243#define BENCHMARK_DEPRECATED_MSG(msg)
244#define BENCHMARK_WARNING_MSG(msg) \
245 __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
246 __LINE__) ") : warning note: " msg))
247#define BENCHMARK_DISABLE_DEPRECATED_WARNING
248#define BENCHMARK_RESTORE_DEPRECATED_WARNING
252#if defined(__GNUC__) && !defined(__clang__)
253#define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
257#define __has_builtin(x) 0
260#if defined(__GNUC__) || __has_builtin(__builtin_unreachable)
261#define BENCHMARK_UNREACHABLE() __builtin_unreachable()
262#elif defined(_MSC_VER)
263#define BENCHMARK_UNREACHABLE() __assume(false)
265#define BENCHMARK_UNREACHABLE() ((void)0)
268#ifdef BENCHMARK_HAS_CXX11
269#define BENCHMARK_OVERRIDE override
271#define BENCHMARK_OVERRIDE
277#pragma warning(disable : 4251)
281class BenchmarkReporter;
283BENCHMARK_EXPORT
void PrintDefaultHelp();
285BENCHMARK_EXPORT
void Initialize(
int* argc,
char** argv,
286 void (*HelperPrinterf)() = PrintDefaultHelp);
287BENCHMARK_EXPORT
void Shutdown();
291BENCHMARK_EXPORT
bool ReportUnrecognizedArguments(
int argc,
char** argv);
294BENCHMARK_EXPORT std::string GetBenchmarkFilter();
300BENCHMARK_EXPORT
void SetBenchmarkFilter(std::string value);
303BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
308BENCHMARK_EXPORT BenchmarkReporter* CreateDefaultDisplayReporter();
326BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks();
327BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(std::string spec);
329BENCHMARK_EXPORT
size_t
330RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
331BENCHMARK_EXPORT
size_t
332RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::string spec);
334BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(
335 BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter);
336BENCHMARK_EXPORT
size_t
337RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
338 BenchmarkReporter* file_reporter, std::string spec);
342enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
344BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
348BENCHMARK_EXPORT
void SetDefaultTimeUnit(TimeUnit unit);
355 static const int64_t TombstoneValue;
361 total_allocated_bytes(TombstoneValue),
362 net_heap_growth(TombstoneValue) {}
368 int64_t max_bytes_used;
372 int64_t total_allocated_bytes;
377 int64_t net_heap_growth;
383 virtual void Start() = 0;
386 BENCHMARK_DEPRECATED_MSG(
"Use Stop(Result&) instead")
387 virtual
void Stop(Result* result) = 0;
390 BENCHMARK_DISABLE_DEPRECATED_WARNING
391 virtual
void Stop(Result& result) { Stop(&result); }
392 BENCHMARK_RESTORE_DEPRECATED_WARNING
398void RegisterMemoryManager(MemoryManager* memory_manager);
402void AddCustomContext(
const std::string& key,
const std::string& value);
407class BenchmarkFamilies;
409BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext();
412void UseCharPointer(
char const volatile*);
416BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*);
419BENCHMARK_EXPORT
int InitializeStreams();
420BENCHMARK_UNUSED
static int stream_init_anchor = InitializeStreams();
424#if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \
425 defined(__EMSCRIPTEN__)
426#define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
431#ifdef BENCHMARK_HAS_CXX11
432inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
433 std::atomic_signal_fence(std::memory_order_acq_rel);
441#ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
442#if !defined(__GNUC__) || defined(__llvm__) || defined(__INTEL_COMPILER)
444inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
445 asm volatile(
"" : :
"r,m"(value) :
"memory");
449inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
450#if defined(__clang__)
451 asm volatile(
"" :
"+r,m"(value) : :
"memory");
453 asm volatile(
"" :
"+m,r"(value) : :
"memory");
456#elif defined(BENCHMARK_HAS_CXX11) && (__GNUC__ >= 5)
460inline BENCHMARK_ALWAYS_INLINE
461 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
462 (
sizeof(Tp) <=
sizeof(Tp*))>::type
463 DoNotOptimize(Tp
const& value) {
464 asm volatile(
"" : :
"r,m"(value) :
"memory");
468inline BENCHMARK_ALWAYS_INLINE
469 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
470 (
sizeof(Tp) >
sizeof(Tp*))>::type
471 DoNotOptimize(Tp
const& value) {
472 asm volatile(
"" : :
"m"(value) :
"memory");
476inline BENCHMARK_ALWAYS_INLINE
477 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
478 (
sizeof(Tp) <=
sizeof(Tp*))>::type
479 DoNotOptimize(Tp& value) {
480 asm volatile(
"" :
"+m,r"(value) : :
"memory");
484inline BENCHMARK_ALWAYS_INLINE
485 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
486 (
sizeof(Tp) >
sizeof(Tp*))>::type
487 DoNotOptimize(Tp& value) {
488 asm volatile(
"" :
"+m"(value) : :
"memory");
496inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
497 asm volatile(
"" : :
"m"(value) :
"memory");
501inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
502 asm volatile(
"" :
"+m"(value) : :
"memory");
506#ifndef BENCHMARK_HAS_CXX11
507inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
508 asm volatile(
"" : : :
"memory");
511#elif defined(_MSC_VER)
513inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
514 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
518#ifndef BENCHMARK_HAS_CXX11
519inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() { _ReadWriteBarrier(); }
523inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp
const& value) {
524 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
539 kAvgThreads = 1 << 1,
541 kAvgThreadsRate = kIsRate | kAvgThreads,
544 kIsIterationInvariant = 1 << 2,
548 kIsIterationInvariantRate = kIsRate | kIsIterationInvariant,
551 kAvgIterations = 1 << 3,
553 kAvgIterationsRate = kIsRate | kAvgIterations,
570 BENCHMARK_ALWAYS_INLINE
571 Counter(
double v = 0., Flags f = kDefaults, OneK k = kIs1000)
572 : value(v), flags(f), oneK(k) {}
574 BENCHMARK_ALWAYS_INLINE
operator double const &()
const {
return value; }
575 BENCHMARK_ALWAYS_INLINE
operator double&() {
return value; }
580Counter::Flags
inline operator|(
const Counter::Flags& LHS,
581 const Counter::Flags& RHS) {
582 return static_cast<Counter::Flags
>(
static_cast<int>(LHS) |
583 static_cast<int>(RHS));
587typedef std::map<std::string, Counter> UserCounters;
593enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
595typedef int64_t IterationCount;
597enum StatisticUnit { kTime, kPercentage };
601typedef double(BigOFunc)(IterationCount);
605typedef double(StatisticsFunc)(
const std::vector<double>&);
610 StatisticsFunc* compute_;
613 Statistics(
const std::string& name, StatisticsFunc* compute,
614 StatisticUnit unit = kTime)
615 : name_(name), compute_(compute), unit_(unit) {}
623enum AggregationReportMode
624#if defined(BENCHMARK_HAS_CXX11)
633 ARM_Default = 1U << 0U,
635 ARM_FileReportAggregatesOnly = 1U << 1U,
637 ARM_DisplayReportAggregatesOnly = 1U << 2U,
639 ARM_ReportAggregatesOnly =
640 ARM_FileReportAggregatesOnly | ARM_DisplayReportAggregatesOnly
677 bool KeepRunningBatch(IterationCount n);
725 void SkipWithError(
const char* msg);
728 bool error_occurred()
const {
return error_occurred_; }
737 void SetIterationTime(
double seconds);
744 BENCHMARK_ALWAYS_INLINE
745 void SetBytesProcessed(int64_t bytes) {
746 counters[
"bytes_per_second"] =
747 Counter(
static_cast<double>(bytes), Counter::kIsRate, Counter::kIs1024);
750 BENCHMARK_ALWAYS_INLINE
751 int64_t bytes_processed()
const {
752 if (counters.find(
"bytes_per_second") != counters.end())
753 return static_cast<int64_t
>(counters.at(
"bytes_per_second"));
762 BENCHMARK_ALWAYS_INLINE
763 void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; }
765 BENCHMARK_ALWAYS_INLINE
766 int64_t complexity_length_n()
const {
return complexity_n_; }
774 BENCHMARK_ALWAYS_INLINE
775 void SetItemsProcessed(int64_t items) {
776 counters[
"items_per_second"] =
777 Counter(
static_cast<double>(items), benchmark::Counter::kIsRate);
780 BENCHMARK_ALWAYS_INLINE
781 int64_t items_processed()
const {
782 if (counters.find(
"items_per_second") != counters.end())
783 return static_cast<int64_t
>(counters.at(
"items_per_second"));
799 void SetLabel(
const char* label);
801 void BENCHMARK_ALWAYS_INLINE SetLabel(
const std::string& str) {
802 this->SetLabel(str.c_str());
806 BENCHMARK_ALWAYS_INLINE
807 int64_t range(std::size_t pos = 0)
const {
808 assert(range_.size() > pos);
812 BENCHMARK_DEPRECATED_MSG(
"use 'range(0)' instead")
813 int64_t range_x()
const {
return range(0); }
815 BENCHMARK_DEPRECATED_MSG(
"use 'range(1)' instead")
816 int64_t range_y()
const {
return range(1); }
819 BENCHMARK_ALWAYS_INLINE
820 int threads()
const {
return threads_; }
823 BENCHMARK_ALWAYS_INLINE
824 int thread_index()
const {
return thread_index_; }
826 BENCHMARK_ALWAYS_INLINE
827 IterationCount iterations()
const {
828 if (BENCHMARK_BUILTIN_EXPECT(!started_,
false)) {
831 return max_iterations - total_iterations_ + batch_leftover_;
838 IterationCount total_iterations_;
843 IterationCount batch_leftover_;
846 const IterationCount max_iterations;
851 bool error_occurred_;
854 std::vector<int64_t> range_;
856 int64_t complexity_n_;
860 UserCounters counters;
863 State(IterationCount max_iters,
const std::vector<int64_t>& ranges,
868 void StartKeepRunning();
871 bool KeepRunningInternal(IterationCount n,
bool is_batch);
872 void FinishKeepRunning();
874 const int thread_index_;
884inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunning() {
885 return KeepRunningInternal(1,
false);
888inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningBatch(IterationCount n) {
889 return KeepRunningInternal(n,
true);
892inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningInternal(IterationCount n,
898 assert(is_batch || n == 1);
899 if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n,
true)) {
900 total_iterations_ -= n;
905 if (!error_occurred_ && total_iterations_ >= n) {
906 total_iterations_ -= n;
911 if (is_batch && total_iterations_ != 0) {
912 batch_leftover_ = n - total_iterations_;
913 total_iterations_ = 0;
922 typedef std::forward_iterator_tag iterator_category;
926 typedef std::ptrdiff_t difference_type;
930 BENCHMARK_ALWAYS_INLINE
933 BENCHMARK_ALWAYS_INLINE
935 : cached_(st->error_occurred_ ? 0 : st->max_iterations), parent_(st) {}
938 BENCHMARK_ALWAYS_INLINE
939 Value operator*()
const {
return Value(); }
941 BENCHMARK_ALWAYS_INLINE
942 StateIterator& operator++() {
948 BENCHMARK_ALWAYS_INLINE
949 bool operator!=(StateIterator
const&)
const {
950 if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0,
true))
return true;
951 parent_->FinishKeepRunning();
956 IterationCount cached_;
957 State*
const parent_;
960inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
961 return StateIterator(
this);
963inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
965 return StateIterator();
970typedef void(Function)(State&);
986 Benchmark* Name(
const std::string& name);
999 Benchmark* Range(int64_t start, int64_t limit);
1004 Benchmark* DenseRange(int64_t start, int64_t limit,
int step = 1);
1009 Benchmark* Args(
const std::vector<int64_t>& args);
1014 Benchmark* ArgPair(int64_t x, int64_t y) {
1015 std::vector<int64_t> args;
1024 Benchmark* Ranges(
const std::vector<std::pair<int64_t, int64_t> >& ranges);
1029 Benchmark* ArgsProduct(
const std::vector<std::vector<int64_t> >& arglists);
1032 Benchmark* ArgName(
const std::string& name);
1036 Benchmark* ArgNames(
const std::vector<std::string>& names);
1041 Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
1042 std::vector<std::pair<int64_t, int64_t> > ranges;
1043 ranges.push_back(std::make_pair(lo1, hi1));
1044 ranges.push_back(std::make_pair(lo2, hi2));
1045 return Ranges(ranges);
1072 Benchmark* RangeMultiplier(
int multiplier);
1091 Benchmark* Iterations(IterationCount n);
1102 Benchmark* ReportAggregatesOnly(
bool value =
true);
1105 Benchmark* DisplayAggregatesOnly(
bool value =
true);
1132 Benchmark* Complexity(BigO complexity = benchmark::oAuto);
1136 Benchmark* Complexity(BigOFunc* complexity);
1139 Benchmark* ComputeStatistics(
const std::string& name,
1140 StatisticsFunc* statistics,
1141 StatisticUnit unit = kTime);
1162 Benchmark* ThreadRange(
int min_threads,
int max_threads);
1168 Benchmark* DenseThreadRange(
int min_threads,
int max_threads,
int stride = 1);
1173 virtual void Run(
State& state) = 0;
1175 TimeUnit GetTimeUnit()
const;
1179 void SetName(
const char* name);
1181 int ArgsCnt()
const;
1188 AggregationReportMode aggregation_report_mode_;
1189 std::vector<std::string> arg_names_;
1190 std::vector<std::vector<int64_t> > args_;
1192 TimeUnit time_unit_;
1193 bool use_default_time_unit_;
1195 int range_multiplier_;
1197 double min_warmup_time_;
1198 IterationCount iterations_;
1200 bool measure_process_cpu_time_;
1201 bool use_real_time_;
1202 bool use_manual_time_;
1204 BigOFunc* complexity_lambda_;
1205 std::vector<Statistics> statistics_;
1206 std::vector<int> thread_counts_;
1209 callback_function setup_;
1210 callback_function teardown_;
1213#if defined(BENCHMARK_HAS_CXX11)
1219#if defined(BENCHMARK_HAS_CXX11)
1232 internal::Function* fn);
1234#if defined(BENCHMARK_HAS_CXX11)
1235template <
class Lambda>
1241BENCHMARK_EXPORT
void ClearRegisteredBenchmarks();
1251 virtual void Run(
State& st) BENCHMARK_OVERRIDE;
1257#ifdef BENCHMARK_HAS_CXX11
1258template <
class Lambda>
1259class LambdaBenchmark :
public Benchmark {
1261 virtual void Run(
State& st) BENCHMARK_OVERRIDE { lambda_(st); }
1264 template <
class OLambda>
1265 LambdaBenchmark(
const char* name, OLambda&& lam)
1266 : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
1268 LambdaBenchmark(LambdaBenchmark
const&) =
delete;
1270 template <
class Lam>
1271 friend Benchmark* ::benchmark::RegisterBenchmark(
const char*, Lam&&);
1278inline internal::Benchmark* RegisterBenchmark(
const char* name,
1279 internal::Function* fn) {
1280 return internal::RegisterBenchmarkInternal(
1281 ::new internal::FunctionBenchmark(name, fn));
1284#ifdef BENCHMARK_HAS_CXX11
1285template <
class Lambda>
1286internal::Benchmark* RegisterBenchmark(
const char* name, Lambda&& fn) {
1288 internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
1289 return internal::RegisterBenchmarkInternal(
1290 ::new BenchType(name, std::forward<Lambda>(fn)));
1294#if defined(BENCHMARK_HAS_CXX11) && \
1295 (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
1296template <
class Lambda,
class... Args>
1297internal::Benchmark* RegisterBenchmark(
const char* name, Lambda&& fn,
1299 return benchmark::RegisterBenchmark(
1303#define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
1311 virtual void Run(
State& st) BENCHMARK_OVERRIDE {
1313 this->BenchmarkCase(st);
1318 virtual void SetUp(
const State&) {}
1319 virtual void TearDown(
const State&) {}
1321 virtual void SetUp(
State& st) { SetUp(
const_cast<const State&
>(st)); }
1322 virtual void TearDown(
State& st) { TearDown(
const_cast<const State&
>(st)); }
1325 virtual void BenchmarkCase(
State&) = 0;
1335#if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
1336#define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
1338#define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
1342#ifdef BENCHMARK_HAS_CXX11
1343#define BENCHMARK_PRIVATE_NAME(...) \
1344 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, \
1347#define BENCHMARK_PRIVATE_NAME(n) \
1348 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
1351#define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
1352#define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
1354#define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
1355 BaseClass##_##Method##_Benchmark
1357#define BENCHMARK_PRIVATE_DECLARE(n) \
1358 static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
1361#ifdef BENCHMARK_HAS_CXX11
1362#define BENCHMARK(...) \
1363 BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1364 (::benchmark::internal::RegisterBenchmarkInternal( \
1365 new ::benchmark::internal::FunctionBenchmark(#__VA_ARGS__, \
1368#define BENCHMARK(n) \
1369 BENCHMARK_PRIVATE_DECLARE(n) = \
1370 (::benchmark::internal::RegisterBenchmarkInternal( \
1371 new ::benchmark::internal::FunctionBenchmark(#n, n)))
1375#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
1376#define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
1377#define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
1378#define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
1379#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
1380 BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
1382#ifdef BENCHMARK_HAS_CXX11
1395#define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1396 BENCHMARK_PRIVATE_DECLARE(func) = \
1397 (::benchmark::internal::RegisterBenchmarkInternal( \
1398 new ::benchmark::internal::FunctionBenchmark( \
1399 #func "/" #test_case_name, \
1400 [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
1412#define BENCHMARK_TEMPLATE1(n, a) \
1413 BENCHMARK_PRIVATE_DECLARE(n) = \
1414 (::benchmark::internal::RegisterBenchmarkInternal( \
1415 new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
1417#define BENCHMARK_TEMPLATE2(n, a, b) \
1418 BENCHMARK_PRIVATE_DECLARE(n) = \
1419 (::benchmark::internal::RegisterBenchmarkInternal( \
1420 new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
1423#ifdef BENCHMARK_HAS_CXX11
1424#define BENCHMARK_TEMPLATE(n, ...) \
1425 BENCHMARK_PRIVATE_DECLARE(n) = \
1426 (::benchmark::internal::RegisterBenchmarkInternal( \
1427 new ::benchmark::internal::FunctionBenchmark( \
1428 #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1430#define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
1433#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1434 class BaseClass##_##Method##_Benchmark : public BaseClass { \
1436 BaseClass##_##Method##_Benchmark() { \
1437 this->SetName(#BaseClass "/" #Method); \
1441 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1444#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1445 class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1447 BaseClass##_##Method##_Benchmark() { \
1448 this->SetName(#BaseClass "<" #a ">/" #Method); \
1452 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1455#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1456 class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
1458 BaseClass##_##Method##_Benchmark() { \
1459 this->SetName(#BaseClass "<" #a "," #b ">/" #Method); \
1463 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1466#ifdef BENCHMARK_HAS_CXX11
1467#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
1468 class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1470 BaseClass##_##Method##_Benchmark() { \
1471 this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); \
1475 virtual void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1478#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) \
1479 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a)
1482#define BENCHMARK_DEFINE_F(BaseClass, Method) \
1483 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1484 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1486#define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \
1487 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1488 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1490#define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \
1491 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1492 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1494#ifdef BENCHMARK_HAS_CXX11
1495#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \
1496 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1497 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1499#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) \
1500 BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)
1503#define BENCHMARK_REGISTER_F(BaseClass, Method) \
1504 BENCHMARK_PRIVATE_REGISTER_F(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method))
1506#define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
1507 BENCHMARK_PRIVATE_DECLARE(TestName) = \
1508 (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
1511#define BENCHMARK_F(BaseClass, Method) \
1512 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1513 BENCHMARK_REGISTER_F(BaseClass, Method); \
1514 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1516#define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \
1517 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1518 BENCHMARK_REGISTER_F(BaseClass, Method); \
1519 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1521#define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \
1522 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1523 BENCHMARK_REGISTER_F(BaseClass, Method); \
1524 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1526#ifdef BENCHMARK_HAS_CXX11
1527#define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \
1528 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1529 BENCHMARK_REGISTER_F(BaseClass, Method); \
1530 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1532#define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) \
1533 BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)
1538#define BENCHMARK_MAIN() \
1539 int main(int argc, char** argv) { \
1540 char arg0_default[] = "benchmark"; \
1541 char* args_default = arg0_default; \
1544 argv = &args_default; \
1546 ::benchmark::Initialize(&argc, argv); \
1547 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
1548 ::benchmark::RunSpecifiedBenchmarks(); \
1549 ::benchmark::Shutdown(); \
1552 int main(int, char**)
1557namespace benchmark {
1567 enum Scaling { UNKNOWN, ENABLED, DISABLED };
1571 double cycles_per_second;
1572 std::vector<CacheInfo> caches;
1573 std::vector<double> load_avg;
1579 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
CPUInfo);
1589 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
SystemInfo);
1596 std::string function_name;
1598 std::string min_time;
1599 std::string min_warmup_time;
1600 std::string iterations;
1601 std::string repetitions;
1602 std::string time_type;
1603 std::string threads;
1607 std::string str()
const;
1621 size_t name_field_width;
1622 static const char* executable_name;
1627 static const int64_t no_repetition_index = -1;
1628 enum RunType { RT_Iteration, RT_Aggregate };
1631 : run_type(RT_Iteration),
1632 aggregate_unit(kTime),
1633 error_occurred(
false),
1636 time_unit(GetDefaultTimeUnit()),
1637 real_accumulated_time(0),
1638 cpu_accumulated_time(0),
1639 max_heapbytes_used(0),
1641 complexity_lambda(),
1643 report_big_o(
false),
1645 memory_result(NULL),
1646 allocs_per_iter(0.0) {}
1648 std::string benchmark_name()
const;
1650 int64_t family_index;
1651 int64_t per_family_instance_index;
1653 std::string aggregate_name;
1654 StatisticUnit aggregate_unit;
1655 std::string report_label;
1656 bool error_occurred;
1657 std::string error_message;
1659 IterationCount iterations;
1661 int64_t repetition_index;
1662 int64_t repetitions;
1664 double real_accumulated_time;
1665 double cpu_accumulated_time;
1671 double GetAdjustedRealTime()
const;
1677 double GetAdjustedCPUTime()
const;
1680 double max_heapbytes_used;
1684 BigOFunc* complexity_lambda;
1685 int64_t complexity_n;
1688 const std::vector<internal::Statistics>* statistics;
1694 UserCounters counters;
1698 double allocs_per_iter;
1711 std::vector<BenchmarkReporter::Run> Runs;
1724 virtual bool ReportContext(
const Context& context) = 0;
1733 virtual void ReportRuns(
const std::vector<Run>& report) = 0;
1737 virtual void Finalize() {}
1741 void SetOutputStream(std::ostream* out) {
1743 output_stream_ = out;
1748 void SetErrorStream(std::ostream* err) {
1750 error_stream_ = err;
1753 std::ostream& GetOutputStream()
const {
return *output_stream_; }
1755 std::ostream& GetErrorStream()
const {
return *error_stream_; }
1757 virtual ~BenchmarkReporter();
1762 static void PrintBasicContext(std::ostream* out, Context
const& context);
1765 std::ostream* output_stream_;
1766 std::ostream* error_stream_;
1773 enum OutputOptions {
1777 OO_ColorTabular = OO_Color | OO_Tabular,
1778 OO_Defaults = OO_ColorTabular
1781 : output_options_(opts_), name_field_width_(0), printed_header_(
false) {}
1783 virtual bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1784 virtual void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1787 virtual void PrintRunData(
const Run& report);
1788 virtual void PrintHeader(
const Run& report);
1790 OutputOptions output_options_;
1791 size_t name_field_width_;
1792 UserCounters prev_counters_;
1793 bool printed_header_;
1799 virtual bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1800 virtual void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1801 virtual void Finalize() BENCHMARK_OVERRIDE;
1804 void PrintRunData(
const Run& report);
1809class BENCHMARK_EXPORT BENCHMARK_DEPRECATED_MSG(
1810 "The CSV Reporter will be removed in a future release") CSVReporter
1813 CSVReporter() : printed_header_(false) {}
1814 virtual bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1815 virtual void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1818 void PrintRunData(
const Run& report);
1820 bool printed_header_;
1821 std::set<std::string> user_counter_names_;
1824inline const char* GetTimeUnitString(TimeUnit unit) {
1835 BENCHMARK_UNREACHABLE();
1838inline double GetTimeUnitMultiplier(TimeUnit unit) {
1849 BENCHMARK_UNREACHABLE();
1862std::vector<int64_t> CreateRange(int64_t lo, int64_t hi,
int multi);
1866std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit,
int step);
1870#if defined(_MSC_VER)
Definition: benchmark.h:1615
Definition: benchmark.h:1771
Definition: benchmark.h:530
Definition: benchmark.h:1307
Definition: benchmark.h:1796
Definition: benchmark.h:353
Definition: benchmark.h:647
Definition: benchmark_register.cc:73
Definition: benchmark_api_internal.h:18
Definition: benchmark.h:978
Definition: benchmark.h:1246
Definition: perf_counters.h:134
Definition: thread_manager.h:12
Definition: thread_timer.h:10
Definition: benchmark.h:1595
Definition: benchmark.h:1617
Definition: benchmark.h:1701
Definition: benchmark.h:1626
Definition: benchmark.h:1560
Definition: benchmark.h:1559
Definition: benchmark.h:357
Definition: benchmark.h:921
Definition: benchmark.h:920
Definition: benchmark.h:1583
Definition: benchmark.h:608