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
224#if defined(__clang__)
225#define BENCHMARK_DONT_OPTIMIZE __attribute__((optnone))
226#elif defined(__GNUC__) || defined(__GNUG__)
227#define BENCHMARK_DONT_OPTIMIZE __attribute__((optimize(0)))
230#define BENCHMARK_DONT_OPTIMIZE
233#if defined(__GNUC__) || defined(__clang__)
234#define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline))
235#elif defined(_MSC_VER) && !defined(__clang__)
236#define BENCHMARK_ALWAYS_INLINE __forceinline
237#define __func__ __FUNCTION__
239#define BENCHMARK_ALWAYS_INLINE
242#define BENCHMARK_INTERNAL_TOSTRING2(x) #x
243#define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
246#if (defined(__GNUC__) && !defined(__NVCC__) && !defined(__NVCOMPILER)) || defined(__clang__)
247#define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
248#define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
249#define BENCHMARK_DISABLE_DEPRECATED_WARNING \
250 _Pragma("GCC diagnostic push") \
251 _Pragma("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
252#define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("GCC diagnostic pop")
253#elif defined(__NVCOMPILER)
254#define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
255#define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
256#define BENCHMARK_DISABLE_DEPRECATED_WARNING \
257 _Pragma("diagnostic push") \
258 _Pragma("diag_suppress deprecated_entity_with_custom_message")
259#define BENCHMARK_RESTORE_DEPRECATED_WARNING _Pragma("diagnostic pop")
261#define BENCHMARK_BUILTIN_EXPECT(x, y) x
262#define BENCHMARK_DEPRECATED_MSG(msg)
263#define BENCHMARK_WARNING_MSG(msg) \
264 __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING( \
265 __LINE__) ") : warning note: " msg))
266#define BENCHMARK_DISABLE_DEPRECATED_WARNING
267#define BENCHMARK_RESTORE_DEPRECATED_WARNING
271#if defined(__GNUC__) && !defined(__clang__)
272#define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
276#define __has_builtin(x) 0
279#if defined(__GNUC__) || __has_builtin(__builtin_unreachable)
280#define BENCHMARK_UNREACHABLE() __builtin_unreachable()
281#elif defined(_MSC_VER)
282#define BENCHMARK_UNREACHABLE() __assume(false)
284#define BENCHMARK_UNREACHABLE() ((void)0)
287#ifdef BENCHMARK_HAS_CXX11
288#define BENCHMARK_OVERRIDE override
290#define BENCHMARK_OVERRIDE
296#pragma warning(disable : 4251)
300class BenchmarkReporter;
303const char kDefaultMinTimeStr[] =
"0.5s";
305BENCHMARK_EXPORT
void PrintDefaultHelp();
307BENCHMARK_EXPORT
void Initialize(
int* argc,
char** argv,
308 void (*HelperPrinterf)() = PrintDefaultHelp);
309BENCHMARK_EXPORT
void Shutdown();
313BENCHMARK_EXPORT
bool ReportUnrecognizedArguments(
int argc,
char** argv);
316BENCHMARK_EXPORT std::string GetBenchmarkFilter();
322BENCHMARK_EXPORT
void SetBenchmarkFilter(std::string value);
325BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
330BENCHMARK_EXPORT BenchmarkReporter* CreateDefaultDisplayReporter();
348BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks();
349BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(std::string spec);
351BENCHMARK_EXPORT
size_t
352RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
353BENCHMARK_EXPORT
size_t
354RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::string spec);
356BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(
357 BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter);
358BENCHMARK_EXPORT
size_t
359RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
360 BenchmarkReporter* file_reporter, std::string spec);
364enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
366BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
370BENCHMARK_EXPORT
void SetDefaultTimeUnit(TimeUnit unit);
377 static const int64_t TombstoneValue;
383 total_allocated_bytes(TombstoneValue),
384 net_heap_growth(TombstoneValue) {}
390 int64_t max_bytes_used;
394 int64_t total_allocated_bytes;
399 int64_t net_heap_growth;
405 virtual void Start() = 0;
408 virtual void Stop(Result& result) = 0;
414void RegisterMemoryManager(MemoryManager* memory_manager);
418void AddCustomContext(
const std::string& key,
const std::string& value);
423class BenchmarkFamilies;
425BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext();
428void UseCharPointer(
char const volatile*);
432BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*);
435BENCHMARK_EXPORT
int InitializeStreams();
436BENCHMARK_UNUSED
static int stream_init_anchor = InitializeStreams();
440#if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \
441 defined(__EMSCRIPTEN__)
442#define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
447#ifdef BENCHMARK_HAS_CXX11
448inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
449 std::atomic_signal_fence(std::memory_order_acq_rel);
457#ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
458#if !defined(__GNUC__) || defined(__llvm__) || defined(__INTEL_COMPILER)
460BENCHMARK_DEPRECATED_MSG(
461 "The const-ref version of this method can permit "
462 "undesired compiler optimizations in benchmarks")
463inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
464 asm volatile(
"" : :
"r,m"(value) :
"memory");
468inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
469#if defined(__clang__)
470 asm volatile(
"" :
"+r,m"(value) : :
"memory");
472 asm volatile(
"" :
"+m,r"(value) : :
"memory");
475#elif defined(BENCHMARK_HAS_CXX11) && (__GNUC__ >= 5)
479BENCHMARK_DEPRECATED_MSG(
480 "The const-ref version of this method can permit "
481 "undesired compiler optimizations in benchmarks")
482inline BENCHMARK_ALWAYS_INLINE
483 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
484 (sizeof(Tp) <= sizeof(Tp*))>::type
485 DoNotOptimize(Tp const& value) {
486 asm volatile(
"" : :
"r,m"(value) :
"memory");
490BENCHMARK_DEPRECATED_MSG(
491 "The const-ref version of this method can permit "
492 "undesired compiler optimizations in benchmarks")
493inline BENCHMARK_ALWAYS_INLINE
494 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
495 (sizeof(Tp) > sizeof(Tp*))>::type
496 DoNotOptimize(Tp const& value) {
497 asm volatile(
"" : :
"m"(value) :
"memory");
501inline BENCHMARK_ALWAYS_INLINE
502 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
503 (
sizeof(Tp) <=
sizeof(Tp*))>::type
504 DoNotOptimize(Tp& value) {
505 asm volatile(
"" :
"+m,r"(value) : :
"memory");
509inline BENCHMARK_ALWAYS_INLINE
510 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
511 (
sizeof(Tp) >
sizeof(Tp*))>::type
512 DoNotOptimize(Tp& value) {
513 asm volatile(
"" :
"+m"(value) : :
"memory");
521BENCHMARK_DEPRECATED_MSG(
522 "The const-ref version of this method can permit "
523 "undesired compiler optimizations in benchmarks")
524inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
525 asm volatile(
"" : :
"m"(value) :
"memory");
529inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
530 asm volatile(
"" :
"+m"(value) : :
"memory");
534#ifndef BENCHMARK_HAS_CXX11
535inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
536 asm volatile(
"" : : :
"memory");
539#elif defined(_MSC_VER)
541BENCHMARK_DEPRECATED_MSG(
542 "The const-ref version of this method can permit "
543 "undesired compiler optimizations in benchmarks")
544inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
545 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
549#ifndef BENCHMARK_HAS_CXX11
550inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() { _ReadWriteBarrier(); }
554BENCHMARK_DEPRECATED_MSG(
555 "The const-ref version of this method can permit "
556 "undesired compiler optimizations in benchmarks")
557inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
558 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
573 kAvgThreads = 1 << 1,
575 kAvgThreadsRate = kIsRate | kAvgThreads,
578 kIsIterationInvariant = 1 << 2,
582 kIsIterationInvariantRate = kIsRate | kIsIterationInvariant,
585 kAvgIterations = 1 << 3,
587 kAvgIterationsRate = kIsRate | kAvgIterations,
604 BENCHMARK_ALWAYS_INLINE
605 Counter(
double v = 0., Flags f = kDefaults, OneK k = kIs1000)
606 : value(v), flags(f), oneK(k) {}
608 BENCHMARK_ALWAYS_INLINE
operator double const &()
const {
return value; }
609 BENCHMARK_ALWAYS_INLINE
operator double&() {
return value; }
614Counter::Flags
inline operator|(
const Counter::Flags& LHS,
615 const Counter::Flags& RHS) {
616 return static_cast<Counter::Flags
>(
static_cast<int>(LHS) |
617 static_cast<int>(RHS));
621typedef std::map<std::string, Counter> UserCounters;
627enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
629typedef int64_t IterationCount;
631enum StatisticUnit { kTime, kPercentage };
635typedef double(BigOFunc)(IterationCount);
639typedef double(StatisticsFunc)(
const std::vector<double>&);
644 StatisticsFunc* compute_;
647 Statistics(
const std::string& name, StatisticsFunc* compute,
648 StatisticUnit unit = kTime)
649 : name_(name), compute_(compute), unit_(unit) {}
657enum AggregationReportMode
658#if defined(BENCHMARK_HAS_CXX11)
667 ARM_Default = 1U << 0U,
669 ARM_FileReportAggregatesOnly = 1U << 1U,
671 ARM_DisplayReportAggregatesOnly = 1U << 2U,
673 ARM_ReportAggregatesOnly =
674 ARM_FileReportAggregatesOnly | ARM_DisplayReportAggregatesOnly
678#if defined(BENCHMARK_HAS_CXX11)
721 bool KeepRunningBatch(IterationCount n);
770 void SkipWithMessage(
const std::string& msg);
791 void SkipWithError(
const std::string& msg);
794 bool skipped()
const {
return internal::NotSkipped != skipped_; }
797 bool error_occurred()
const {
return internal::SkippedWithError == skipped_; }
806 void SetIterationTime(
double seconds);
813 BENCHMARK_ALWAYS_INLINE
814 void SetBytesProcessed(int64_t bytes) {
815 counters[
"bytes_per_second"] =
816 Counter(
static_cast<double>(bytes), Counter::kIsRate, Counter::kIs1024);
819 BENCHMARK_ALWAYS_INLINE
820 int64_t bytes_processed()
const {
821 if (counters.find(
"bytes_per_second") != counters.end())
822 return static_cast<int64_t
>(counters.at(
"bytes_per_second"));
831 BENCHMARK_ALWAYS_INLINE
832 void SetComplexityN(int64_t complexity_n) { complexity_n_ = complexity_n; }
834 BENCHMARK_ALWAYS_INLINE
835 int64_t complexity_length_n()
const {
return complexity_n_; }
843 BENCHMARK_ALWAYS_INLINE
844 void SetItemsProcessed(int64_t items) {
845 counters[
"items_per_second"] =
846 Counter(
static_cast<double>(items), benchmark::Counter::kIsRate);
849 BENCHMARK_ALWAYS_INLINE
850 int64_t items_processed()
const {
851 if (counters.find(
"items_per_second") != counters.end())
852 return static_cast<int64_t
>(counters.at(
"items_per_second"));
868 void SetLabel(
const std::string& label);
871 BENCHMARK_ALWAYS_INLINE
872 int64_t range(std::size_t pos = 0)
const {
873 assert(range_.size() > pos);
877 BENCHMARK_DEPRECATED_MSG(
"use 'range(0)' instead")
878 int64_t range_x()
const {
return range(0); }
880 BENCHMARK_DEPRECATED_MSG(
"use 'range(1)' instead")
881 int64_t range_y()
const {
return range(1); }
884 BENCHMARK_ALWAYS_INLINE
885 int threads()
const {
return threads_; }
888 BENCHMARK_ALWAYS_INLINE
889 int thread_index()
const {
return thread_index_; }
891 BENCHMARK_ALWAYS_INLINE
892 IterationCount iterations()
const {
893 if (BENCHMARK_BUILTIN_EXPECT(!started_,
false)) {
896 return max_iterations - total_iterations_ + batch_leftover_;
899 BENCHMARK_ALWAYS_INLINE
900 std::string name()
const {
return name_; }
906 IterationCount total_iterations_;
911 IterationCount batch_leftover_;
914 const IterationCount max_iterations;
919 internal::Skipped skipped_;
922 std::vector<int64_t> range_;
924 int64_t complexity_n_;
928 UserCounters counters;
931 State(std::string name, IterationCount max_iters,
932 const std::vector<int64_t>& ranges,
int thread_i,
int n_threads,
936 void StartKeepRunning();
939 bool KeepRunningInternal(IterationCount n,
bool is_batch);
940 void FinishKeepRunning();
942 const std::string name_;
943 const int thread_index_;
953inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunning() {
954 return KeepRunningInternal(1,
false);
957inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningBatch(IterationCount n) {
958 return KeepRunningInternal(n,
true);
961inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningInternal(IterationCount n,
967 assert(is_batch || n == 1);
968 if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n,
true)) {
969 total_iterations_ -= n;
974 if (!skipped() && total_iterations_ >= n) {
975 total_iterations_ -= n;
980 if (is_batch && total_iterations_ != 0) {
981 batch_leftover_ = n - total_iterations_;
982 total_iterations_ = 0;
991 typedef std::forward_iterator_tag iterator_category;
995 typedef std::ptrdiff_t difference_type;
999 BENCHMARK_ALWAYS_INLINE
1002 BENCHMARK_ALWAYS_INLINE
1004 : cached_(st->skipped() ? 0 : st->max_iterations), parent_(st) {}
1007 BENCHMARK_ALWAYS_INLINE
1008 Value operator*()
const {
return Value(); }
1010 BENCHMARK_ALWAYS_INLINE
1011 StateIterator& operator++() {
1012 assert(cached_ > 0);
1017 BENCHMARK_ALWAYS_INLINE
1018 bool operator!=(StateIterator
const&)
const {
1019 if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0,
true))
return true;
1020 parent_->FinishKeepRunning();
1025 IterationCount cached_;
1026 State*
const parent_;
1029inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
1030 return StateIterator(
this);
1032inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
1034 return StateIterator();
1039typedef void(Function)(State&);
1055 Benchmark* Name(
const std::string& name);
1068 Benchmark* Range(int64_t start, int64_t limit);
1073 Benchmark* DenseRange(int64_t start, int64_t limit,
int step = 1);
1078 Benchmark* Args(
const std::vector<int64_t>& args);
1083 Benchmark* ArgPair(int64_t x, int64_t y) {
1084 std::vector<int64_t> args;
1093 Benchmark* Ranges(
const std::vector<std::pair<int64_t, int64_t> >& ranges);
1098 Benchmark* ArgsProduct(
const std::vector<std::vector<int64_t> >& arglists);
1101 Benchmark* ArgName(
const std::string& name);
1105 Benchmark* ArgNames(
const std::vector<std::string>& names);
1110 Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
1111 std::vector<std::pair<int64_t, int64_t> > ranges;
1112 ranges.push_back(std::make_pair(lo1, hi1));
1113 ranges.push_back(std::make_pair(lo2, hi2));
1114 return Ranges(ranges);
1141 Benchmark* RangeMultiplier(
int multiplier);
1161 Benchmark* Iterations(IterationCount n);
1172 Benchmark* ReportAggregatesOnly(
bool value =
true);
1175 Benchmark* DisplayAggregatesOnly(
bool value =
true);
1202 Benchmark* Complexity(BigO complexity = benchmark::oAuto);
1206 Benchmark* Complexity(BigOFunc* complexity);
1209 Benchmark* ComputeStatistics(
const std::string& name,
1210 StatisticsFunc* statistics,
1211 StatisticUnit unit = kTime);
1232 Benchmark* ThreadRange(
int min_threads,
int max_threads);
1238 Benchmark* DenseThreadRange(
int min_threads,
int max_threads,
int stride = 1);
1243 virtual void Run(
State& state) = 0;
1245 TimeUnit GetTimeUnit()
const;
1248 explicit Benchmark(
const std::string& name);
1249 void SetName(
const std::string& name);
1252 const char* GetName()
const;
1253 int ArgsCnt()
const;
1254 const char* GetArgName(
int arg)
const;
1261 AggregationReportMode aggregation_report_mode_;
1262 std::vector<std::string> arg_names_;
1263 std::vector<std::vector<int64_t> > args_;
1265 TimeUnit time_unit_;
1266 bool use_default_time_unit_;
1268 int range_multiplier_;
1270 double min_warmup_time_;
1271 IterationCount iterations_;
1273 bool measure_process_cpu_time_;
1274 bool use_real_time_;
1275 bool use_manual_time_;
1277 BigOFunc* complexity_lambda_;
1278 std::vector<Statistics> statistics_;
1279 std::vector<int> thread_counts_;
1282 callback_function setup_;
1283 callback_function teardown_;
1286#if defined(BENCHMARK_HAS_CXX11)
1292#if defined(BENCHMARK_HAS_CXX11)
1305 internal::Function* fn);
1307#if defined(BENCHMARK_HAS_CXX11)
1308template <
class Lambda>
1314BENCHMARK_EXPORT
void ClearRegisteredBenchmarks();
1324 void Run(
State& st) BENCHMARK_OVERRIDE;
1330#ifdef BENCHMARK_HAS_CXX11
1331template <
class Lambda>
1332class LambdaBenchmark :
public Benchmark {
1334 void Run(
State& st) BENCHMARK_OVERRIDE { lambda_(st); }
1337 template <
class OLambda>
1338 LambdaBenchmark(
const std::string& name, OLambda&& lam)
1339 : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
1341 LambdaBenchmark(LambdaBenchmark
const&) =
delete;
1343 template <
class Lam>
1344 friend Benchmark* ::benchmark::RegisterBenchmark(
const std::string&, Lam&&);
1351inline internal::Benchmark* RegisterBenchmark(
const std::string& name,
1352 internal::Function* fn) {
1353 return internal::RegisterBenchmarkInternal(
1354 ::new internal::FunctionBenchmark(name, fn));
1357#ifdef BENCHMARK_HAS_CXX11
1358template <
class Lambda>
1359internal::Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn) {
1361 internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
1362 return internal::RegisterBenchmarkInternal(
1363 ::new BenchType(name, std::forward<Lambda>(fn)));
1367#if defined(BENCHMARK_HAS_CXX11) && \
1368 (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
1369template <
class Lambda,
class... Args>
1370internal::Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn,
1372 return benchmark::RegisterBenchmark(
1376#define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
1384 void Run(
State& st) BENCHMARK_OVERRIDE {
1386 this->BenchmarkCase(st);
1391 virtual void SetUp(
const State&) {}
1392 virtual void TearDown(
const State&) {}
1394 virtual void SetUp(
State& st) { SetUp(
const_cast<const State&
>(st)); }
1395 virtual void TearDown(
State& st) { TearDown(
const_cast<const State&
>(st)); }
1398 virtual void BenchmarkCase(
State&) = 0;
1408#if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
1409#define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
1411#define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
1415#ifdef BENCHMARK_HAS_CXX11
1416#define BENCHMARK_PRIVATE_NAME(...) \
1417 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, \
1420#define BENCHMARK_PRIVATE_NAME(n) \
1421 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
1424#define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
1425#define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
1427#define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
1428 BaseClass##_##Method##_Benchmark
1430#define BENCHMARK_PRIVATE_DECLARE(n) \
1431 static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
1434#ifdef BENCHMARK_HAS_CXX11
1435#define BENCHMARK(...) \
1436 BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1437 (::benchmark::internal::RegisterBenchmarkInternal( \
1438 new ::benchmark::internal::FunctionBenchmark(#__VA_ARGS__, \
1441#define BENCHMARK(n) \
1442 BENCHMARK_PRIVATE_DECLARE(n) = \
1443 (::benchmark::internal::RegisterBenchmarkInternal( \
1444 new ::benchmark::internal::FunctionBenchmark(#n, n)))
1448#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
1449#define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
1450#define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
1451#define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
1452#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
1453 BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
1455#ifdef BENCHMARK_HAS_CXX11
1468#define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1469 BENCHMARK_PRIVATE_DECLARE(func) = \
1470 (::benchmark::internal::RegisterBenchmarkInternal( \
1471 new ::benchmark::internal::FunctionBenchmark( \
1472 #func "/" #test_case_name, \
1473 [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
1485#define BENCHMARK_TEMPLATE1(n, a) \
1486 BENCHMARK_PRIVATE_DECLARE(n) = \
1487 (::benchmark::internal::RegisterBenchmarkInternal( \
1488 new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
1490#define BENCHMARK_TEMPLATE2(n, a, b) \
1491 BENCHMARK_PRIVATE_DECLARE(n) = \
1492 (::benchmark::internal::RegisterBenchmarkInternal( \
1493 new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
1496#ifdef BENCHMARK_HAS_CXX11
1497#define BENCHMARK_TEMPLATE(n, ...) \
1498 BENCHMARK_PRIVATE_DECLARE(n) = \
1499 (::benchmark::internal::RegisterBenchmarkInternal( \
1500 new ::benchmark::internal::FunctionBenchmark( \
1501 #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1503#define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
1506#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1507 class BaseClass##_##Method##_Benchmark : public BaseClass { \
1509 BaseClass##_##Method##_Benchmark() { \
1510 this->SetName(#BaseClass "/" #Method); \
1514 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1517#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1518 class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1520 BaseClass##_##Method##_Benchmark() { \
1521 this->SetName(#BaseClass "<" #a ">/" #Method); \
1525 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1528#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1529 class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
1531 BaseClass##_##Method##_Benchmark() { \
1532 this->SetName(#BaseClass "<" #a "," #b ">/" #Method); \
1536 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1539#ifdef BENCHMARK_HAS_CXX11
1540#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
1541 class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1543 BaseClass##_##Method##_Benchmark() { \
1544 this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); \
1548 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1551#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) \
1552 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a)
1555#define BENCHMARK_DEFINE_F(BaseClass, Method) \
1556 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1557 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1559#define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \
1560 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1561 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1563#define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \
1564 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1565 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1567#ifdef BENCHMARK_HAS_CXX11
1568#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \
1569 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1570 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1572#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) \
1573 BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)
1576#define BENCHMARK_REGISTER_F(BaseClass, Method) \
1577 BENCHMARK_PRIVATE_REGISTER_F(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method))
1579#define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
1580 BENCHMARK_PRIVATE_DECLARE(TestName) = \
1581 (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
1584#define BENCHMARK_F(BaseClass, Method) \
1585 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1586 BENCHMARK_REGISTER_F(BaseClass, Method); \
1587 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1589#define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \
1590 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1591 BENCHMARK_REGISTER_F(BaseClass, Method); \
1592 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1594#define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \
1595 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1596 BENCHMARK_REGISTER_F(BaseClass, Method); \
1597 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1599#ifdef BENCHMARK_HAS_CXX11
1600#define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \
1601 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1602 BENCHMARK_REGISTER_F(BaseClass, Method); \
1603 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1605#define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) \
1606 BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)
1611#define BENCHMARK_MAIN() \
1612 int main(int argc, char** argv) { \
1613 char arg0_default[] = "benchmark"; \
1614 char* args_default = arg0_default; \
1617 argv = &args_default; \
1619 ::benchmark::Initialize(&argc, argv); \
1620 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
1621 ::benchmark::RunSpecifiedBenchmarks(); \
1622 ::benchmark::Shutdown(); \
1625 int main(int, char**)
1630namespace benchmark {
1640 enum Scaling { UNKNOWN, ENABLED, DISABLED };
1644 double cycles_per_second;
1645 std::vector<CacheInfo> caches;
1646 std::vector<double> load_avg;
1652 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
CPUInfo);
1662 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
SystemInfo);
1669 std::string function_name;
1671 std::string min_time;
1672 std::string min_warmup_time;
1673 std::string iterations;
1674 std::string repetitions;
1675 std::string time_type;
1676 std::string threads;
1680 std::string str()
const;
1694 size_t name_field_width;
1695 static const char* executable_name;
1700 static const int64_t no_repetition_index = -1;
1701 enum RunType { RT_Iteration, RT_Aggregate };
1704 : run_type(RT_Iteration),
1705 aggregate_unit(kTime),
1706 skipped(internal::NotSkipped),
1709 time_unit(GetDefaultTimeUnit()),
1710 real_accumulated_time(0),
1711 cpu_accumulated_time(0),
1712 max_heapbytes_used(0),
1714 complexity_lambda(),
1716 report_big_o(
false),
1718 memory_result(NULL),
1719 allocs_per_iter(0.0) {}
1721 std::string benchmark_name()
const;
1723 int64_t family_index;
1724 int64_t per_family_instance_index;
1726 std::string aggregate_name;
1727 StatisticUnit aggregate_unit;
1728 std::string report_label;
1729 internal::Skipped skipped;
1730 std::string skip_message;
1732 IterationCount iterations;
1734 int64_t repetition_index;
1735 int64_t repetitions;
1737 double real_accumulated_time;
1738 double cpu_accumulated_time;
1744 double GetAdjustedRealTime()
const;
1750 double GetAdjustedCPUTime()
const;
1753 double max_heapbytes_used;
1757 BigOFunc* complexity_lambda;
1758 int64_t complexity_n;
1761 const std::vector<internal::Statistics>* statistics;
1767 UserCounters counters;
1771 double allocs_per_iter;
1784 std::vector<BenchmarkReporter::Run> Runs;
1797 virtual bool ReportContext(
const Context& context) = 0;
1801 virtual void ReportRunsConfig(
double ,
1812 virtual void ReportRuns(
const std::vector<Run>& report) = 0;
1816 virtual void Finalize() {}
1820 void SetOutputStream(std::ostream* out) {
1822 output_stream_ = out;
1827 void SetErrorStream(std::ostream* err) {
1829 error_stream_ = err;
1832 std::ostream& GetOutputStream()
const {
return *output_stream_; }
1834 std::ostream& GetErrorStream()
const {
return *error_stream_; }
1836 virtual ~BenchmarkReporter();
1841 static void PrintBasicContext(std::ostream* out, Context
const& context);
1844 std::ostream* output_stream_;
1845 std::ostream* error_stream_;
1852 enum OutputOptions {
1856 OO_ColorTabular = OO_Color | OO_Tabular,
1857 OO_Defaults = OO_ColorTabular
1860 : output_options_(opts_), name_field_width_(0), printed_header_(
false) {}
1862 bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1863 void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1866 virtual void PrintRunData(
const Run& report);
1867 virtual void PrintHeader(
const Run& report);
1869 OutputOptions output_options_;
1870 size_t name_field_width_;
1871 UserCounters prev_counters_;
1872 bool printed_header_;
1878 bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1879 void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1880 void Finalize() BENCHMARK_OVERRIDE;
1883 void PrintRunData(
const Run& report);
1888class BENCHMARK_EXPORT BENCHMARK_DEPRECATED_MSG(
1889 "The CSV Reporter will be removed in a future release") CSVReporter
1892 CSVReporter() : printed_header_(false) {}
1893 bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1894 void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1897 void PrintRunData(
const Run& report);
1899 bool printed_header_;
1900 std::set<std::string> user_counter_names_;
1903inline const char* GetTimeUnitString(TimeUnit unit) {
1914 BENCHMARK_UNREACHABLE();
1917inline double GetTimeUnitMultiplier(TimeUnit unit) {
1928 BENCHMARK_UNREACHABLE();
1941std::vector<int64_t> CreateRange(int64_t lo, int64_t hi,
int multi);
1945std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit,
int step);
1949#if defined(_MSC_VER)
Definition: benchmark.h:1688
Definition: benchmark.h:1850
Definition: benchmark.h:564
Definition: benchmark.h:1380
Definition: benchmark.h:1875
Definition: benchmark.h:375
Definition: benchmark.h:691
Definition: benchmark_register.cc:73
Definition: benchmark_api_internal.h:18
Definition: benchmark.h:1047
Definition: benchmark.h:1319
Definition: perf_counters.h:149
Definition: thread_manager.h:12
Definition: thread_timer.h:10
Definition: benchmark.h:1668
Definition: benchmark.h:1690
Definition: benchmark.h:1774
Definition: benchmark.h:1699
Definition: benchmark.h:1633
Definition: benchmark.h:1632
Definition: benchmark.h:379
Definition: benchmark.h:990
Definition: benchmark.h:989
Definition: benchmark.h:1656
Definition: benchmark.h:642