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";
306BENCHMARK_EXPORT std::string GetBenchmarkVersion();
308BENCHMARK_EXPORT
void PrintDefaultHelp();
310BENCHMARK_EXPORT
void Initialize(
int* argc,
char** argv,
311 void (*HelperPrinterf)() = PrintDefaultHelp);
312BENCHMARK_EXPORT
void Shutdown();
316BENCHMARK_EXPORT
bool ReportUnrecognizedArguments(
int argc,
char** argv);
319BENCHMARK_EXPORT std::string GetBenchmarkFilter();
325BENCHMARK_EXPORT
void SetBenchmarkFilter(std::string value);
328BENCHMARK_EXPORT int32_t GetBenchmarkVerbosity();
333BENCHMARK_EXPORT BenchmarkReporter* CreateDefaultDisplayReporter();
351BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks();
352BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(std::string spec);
354BENCHMARK_EXPORT
size_t
355RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter);
356BENCHMARK_EXPORT
size_t
357RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter, std::string spec);
359BENCHMARK_EXPORT
size_t RunSpecifiedBenchmarks(
360 BenchmarkReporter* display_reporter, BenchmarkReporter* file_reporter);
361BENCHMARK_EXPORT
size_t
362RunSpecifiedBenchmarks(BenchmarkReporter* display_reporter,
363 BenchmarkReporter* file_reporter, std::string spec);
367enum TimeUnit { kNanosecond, kMicrosecond, kMillisecond, kSecond };
369BENCHMARK_EXPORT TimeUnit GetDefaultTimeUnit();
373BENCHMARK_EXPORT
void SetDefaultTimeUnit(TimeUnit unit);
380 static const int64_t TombstoneValue;
386 total_allocated_bytes(TombstoneValue),
387 net_heap_growth(TombstoneValue) {}
393 int64_t max_bytes_used;
397 int64_t total_allocated_bytes;
402 int64_t net_heap_growth;
408 virtual void Start() = 0;
411 virtual void Stop(Result& result) = 0;
417void RegisterMemoryManager(MemoryManager* memory_manager);
427 virtual void AfterSetupStart() = 0;
431 virtual void BeforeTeardownStop() = 0;
441void AddCustomContext(
const std::string& key,
const std::string& value);
446class BenchmarkFamilies;
448BENCHMARK_EXPORT std::map<std::string, std::string>*& GetGlobalContext();
451void UseCharPointer(
char const volatile*);
455BENCHMARK_EXPORT Benchmark* RegisterBenchmarkInternal(Benchmark*);
458BENCHMARK_EXPORT
int InitializeStreams();
459BENCHMARK_UNUSED
static int stream_init_anchor = InitializeStreams();
463#if (!defined(__GNUC__) && !defined(__clang__)) || defined(__pnacl__) || \
464 defined(__EMSCRIPTEN__)
465#define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
470#ifdef BENCHMARK_HAS_CXX11
471inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
472 std::atomic_signal_fence(std::memory_order_acq_rel);
480#ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
481#if !defined(__GNUC__) || defined(__llvm__) || defined(__INTEL_COMPILER)
483BENCHMARK_DEPRECATED_MSG(
484 "The const-ref version of this method can permit "
485 "undesired compiler optimizations in benchmarks")
486inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
487 asm volatile(
"" : :
"r,m"(value) :
"memory");
491inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
492#if defined(__clang__)
493 asm volatile(
"" :
"+r,m"(value) : :
"memory");
495 asm volatile(
"" :
"+m,r"(value) : :
"memory");
499#ifdef BENCHMARK_HAS_CXX11
501inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp&& value) {
502#if defined(__clang__)
503 asm volatile(
"" :
"+r,m"(value) : :
"memory");
505 asm volatile(
"" :
"+m,r"(value) : :
"memory");
509#elif defined(BENCHMARK_HAS_CXX11) && (__GNUC__ >= 5)
513BENCHMARK_DEPRECATED_MSG(
514 "The const-ref version of this method can permit "
515 "undesired compiler optimizations in benchmarks")
516inline BENCHMARK_ALWAYS_INLINE
517 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
518 (sizeof(Tp) <= sizeof(Tp*))>::type
519 DoNotOptimize(Tp const& value) {
520 asm volatile(
"" : :
"r,m"(value) :
"memory");
524BENCHMARK_DEPRECATED_MSG(
525 "The const-ref version of this method can permit "
526 "undesired compiler optimizations in benchmarks")
527inline BENCHMARK_ALWAYS_INLINE
528 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
529 (sizeof(Tp) > sizeof(Tp*))>::type
530 DoNotOptimize(Tp const& value) {
531 asm volatile(
"" : :
"m"(value) :
"memory");
535inline BENCHMARK_ALWAYS_INLINE
536 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
537 (
sizeof(Tp) <=
sizeof(Tp*))>::type
538 DoNotOptimize(Tp& value) {
539 asm volatile(
"" :
"+m,r"(value) : :
"memory");
543inline BENCHMARK_ALWAYS_INLINE
544 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
545 (
sizeof(Tp) >
sizeof(Tp*))>::type
546 DoNotOptimize(Tp& value) {
547 asm volatile(
"" :
"+m"(value) : :
"memory");
551inline BENCHMARK_ALWAYS_INLINE
552 typename std::enable_if<std::is_trivially_copyable<Tp>::value &&
553 (
sizeof(Tp) <=
sizeof(Tp*))>::type
554 DoNotOptimize(Tp&& value) {
555 asm volatile(
"" :
"+m,r"(value) : :
"memory");
559inline BENCHMARK_ALWAYS_INLINE
560 typename std::enable_if<!std::is_trivially_copyable<Tp>::value ||
561 (
sizeof(Tp) >
sizeof(Tp*))>::type
562 DoNotOptimize(Tp&& value) {
563 asm volatile(
"" :
"+m"(value) : :
"memory");
571BENCHMARK_DEPRECATED_MSG(
572 "The const-ref version of this method can permit "
573 "undesired compiler optimizations in benchmarks")
574inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
575 asm volatile(
"" : :
"m"(value) :
"memory");
579inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
580 asm volatile(
"" :
"+m"(value) : :
"memory");
583#ifdef BENCHMARK_HAS_CXX11
585inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp&& value) {
586 asm volatile(
"" :
"+m"(value) : :
"memory");
591#ifndef BENCHMARK_HAS_CXX11
592inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() {
593 asm volatile(
"" : : :
"memory");
596#elif defined(_MSC_VER)
598BENCHMARK_DEPRECATED_MSG(
599 "The const-ref version of this method can permit "
600 "undesired compiler optimizations in benchmarks")
601inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
602 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
606#ifndef BENCHMARK_HAS_CXX11
607inline BENCHMARK_ALWAYS_INLINE
void ClobberMemory() { _ReadWriteBarrier(); }
610#ifdef BENCHMARK_HAS_CXX11
612inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp&& value) {
613 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
617BENCHMARK_DEPRECATED_MSG(
618 "The const-ref version of this method can permit "
619 "undesired compiler optimizations in benchmarks")
620inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp const& value) {
621 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
625inline BENCHMARK_ALWAYS_INLINE
void DoNotOptimize(Tp& value) {
626 internal::UseCharPointer(&
reinterpret_cast<char const volatile&
>(value));
642 kAvgThreads = 1 << 1,
644 kAvgThreadsRate = kIsRate | kAvgThreads,
647 kIsIterationInvariant = 1 << 2,
651 kIsIterationInvariantRate = kIsRate | kIsIterationInvariant,
654 kAvgIterations = 1 << 3,
656 kAvgIterationsRate = kIsRate | kAvgIterations,
673 BENCHMARK_ALWAYS_INLINE
674 Counter(
double v = 0., Flags f = kDefaults, OneK k = kIs1000)
675 : value(v), flags(f), oneK(k) {}
677 BENCHMARK_ALWAYS_INLINE
operator double const &()
const {
return value; }
678 BENCHMARK_ALWAYS_INLINE
operator double&() {
return value; }
683Counter::Flags
inline operator|(
const Counter::Flags& LHS,
684 const Counter::Flags& RHS) {
685 return static_cast<Counter::Flags
>(
static_cast<int>(LHS) |
686 static_cast<int>(RHS));
690typedef std::map<std::string, Counter> UserCounters;
696enum BigO { oNone, o1, oN, oNSquared, oNCubed, oLogN, oNLogN, oAuto, oLambda };
698typedef int64_t ComplexityN;
700typedef int64_t IterationCount;
702enum StatisticUnit { kTime, kPercentage };
706typedef double(BigOFunc)(ComplexityN);
710typedef double(StatisticsFunc)(
const std::vector<double>&);
715 StatisticsFunc* compute_;
718 Statistics(
const std::string& name, StatisticsFunc* compute,
719 StatisticUnit unit = kTime)
720 : name_(name), compute_(compute), unit_(unit) {}
728enum AggregationReportMode
729#if defined(BENCHMARK_HAS_CXX11)
738 ARM_Default = 1U << 0U,
740 ARM_FileReportAggregatesOnly = 1U << 1U,
742 ARM_DisplayReportAggregatesOnly = 1U << 2U,
744 ARM_ReportAggregatesOnly =
745 ARM_FileReportAggregatesOnly | ARM_DisplayReportAggregatesOnly
749#if defined(BENCHMARK_HAS_CXX11)
780 inline bool KeepRunning();
792 inline bool KeepRunningBatch(IterationCount n);
841 void SkipWithMessage(
const std::string& msg);
862 void SkipWithError(
const std::string& msg);
865 bool skipped()
const {
return internal::NotSkipped != skipped_; }
868 bool error_occurred()
const {
return internal::SkippedWithError == skipped_; }
877 void SetIterationTime(
double seconds);
884 BENCHMARK_ALWAYS_INLINE
885 void SetBytesProcessed(int64_t bytes) {
886 counters[
"bytes_per_second"] =
887 Counter(
static_cast<double>(bytes), Counter::kIsRate, Counter::kIs1024);
890 BENCHMARK_ALWAYS_INLINE
891 int64_t bytes_processed()
const {
892 if (counters.find(
"bytes_per_second") != counters.end())
893 return static_cast<int64_t
>(counters.at(
"bytes_per_second"));
902 BENCHMARK_ALWAYS_INLINE
903 void SetComplexityN(ComplexityN complexity_n) {
904 complexity_n_ = complexity_n;
907 BENCHMARK_ALWAYS_INLINE
908 ComplexityN complexity_length_n()
const {
return complexity_n_; }
916 BENCHMARK_ALWAYS_INLINE
917 void SetItemsProcessed(int64_t items) {
918 counters[
"items_per_second"] =
919 Counter(
static_cast<double>(items), benchmark::Counter::kIsRate);
922 BENCHMARK_ALWAYS_INLINE
923 int64_t items_processed()
const {
924 if (counters.find(
"items_per_second") != counters.end())
925 return static_cast<int64_t
>(counters.at(
"items_per_second"));
941 void SetLabel(
const std::string& label);
944 BENCHMARK_ALWAYS_INLINE
945 int64_t range(std::size_t pos = 0)
const {
946 assert(range_.size() > pos);
950 BENCHMARK_DEPRECATED_MSG(
"use 'range(0)' instead")
951 int64_t range_x()
const {
return range(0); }
953 BENCHMARK_DEPRECATED_MSG(
"use 'range(1)' instead")
954 int64_t range_y()
const {
return range(1); }
957 BENCHMARK_ALWAYS_INLINE
958 int threads()
const {
return threads_; }
961 BENCHMARK_ALWAYS_INLINE
962 int thread_index()
const {
return thread_index_; }
964 BENCHMARK_ALWAYS_INLINE
965 IterationCount iterations()
const {
966 if (BENCHMARK_BUILTIN_EXPECT(!started_,
false)) {
969 return max_iterations - total_iterations_ + batch_leftover_;
972 BENCHMARK_ALWAYS_INLINE
973 std::string name()
const {
return name_; }
979 IterationCount total_iterations_;
984 IterationCount batch_leftover_;
987 const IterationCount max_iterations;
992 internal::Skipped skipped_;
995 std::vector<int64_t> range_;
997 ComplexityN complexity_n_;
1001 UserCounters counters;
1004 State(std::string name, IterationCount max_iters,
1005 const std::vector<int64_t>& ranges,
int thread_i,
int n_threads,
1010 void StartKeepRunning();
1013 inline bool KeepRunningInternal(IterationCount n,
bool is_batch);
1014 void FinishKeepRunning();
1016 const std::string name_;
1017 const int thread_index_;
1028inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunning() {
1029 return KeepRunningInternal(1,
false);
1032inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningBatch(IterationCount n) {
1033 return KeepRunningInternal(n,
true);
1036inline BENCHMARK_ALWAYS_INLINE
bool State::KeepRunningInternal(IterationCount n,
1042 assert(is_batch || n == 1);
1043 if (BENCHMARK_BUILTIN_EXPECT(total_iterations_ >= n,
true)) {
1044 total_iterations_ -= n;
1049 if (!skipped() && total_iterations_ >= n) {
1050 total_iterations_ -= n;
1055 if (is_batch && total_iterations_ != 0) {
1056 batch_leftover_ = n - total_iterations_;
1057 total_iterations_ = 0;
1060 FinishKeepRunning();
1066 typedef std::forward_iterator_tag iterator_category;
1070 typedef std::ptrdiff_t difference_type;
1074 BENCHMARK_ALWAYS_INLINE
1077 BENCHMARK_ALWAYS_INLINE
1079 : cached_(st->skipped() ? 0 : st->max_iterations), parent_(st) {}
1082 BENCHMARK_ALWAYS_INLINE
1083 Value operator*()
const {
return Value(); }
1085 BENCHMARK_ALWAYS_INLINE
1086 StateIterator& operator++() {
1087 assert(cached_ > 0);
1092 BENCHMARK_ALWAYS_INLINE
1093 bool operator!=(StateIterator
const&)
const {
1094 if (BENCHMARK_BUILTIN_EXPECT(cached_ != 0,
true))
return true;
1095 parent_->FinishKeepRunning();
1100 IterationCount cached_;
1101 State*
const parent_;
1104inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::begin() {
1105 return StateIterator(
this);
1107inline BENCHMARK_ALWAYS_INLINE State::StateIterator State::end() {
1109 return StateIterator();
1114typedef void(Function)(State&);
1130 Benchmark* Name(
const std::string& name);
1143 Benchmark* Range(int64_t start, int64_t limit);
1148 Benchmark* DenseRange(int64_t start, int64_t limit,
int step = 1);
1153 Benchmark* Args(
const std::vector<int64_t>& args);
1158 Benchmark* ArgPair(int64_t x, int64_t y) {
1159 std::vector<int64_t> args;
1168 Benchmark* Ranges(
const std::vector<std::pair<int64_t, int64_t> >& ranges);
1173 Benchmark* ArgsProduct(
const std::vector<std::vector<int64_t> >& arglists);
1176 Benchmark* ArgName(
const std::string& name);
1180 Benchmark* ArgNames(
const std::vector<std::string>& names);
1185 Benchmark* RangePair(int64_t lo1, int64_t hi1, int64_t lo2, int64_t hi2) {
1186 std::vector<std::pair<int64_t, int64_t> > ranges;
1187 ranges.push_back(std::make_pair(lo1, hi1));
1188 ranges.push_back(std::make_pair(lo2, hi2));
1189 return Ranges(ranges);
1216 Benchmark* RangeMultiplier(
int multiplier);
1236 Benchmark* Iterations(IterationCount n);
1247 Benchmark* ReportAggregatesOnly(
bool value =
true);
1250 Benchmark* DisplayAggregatesOnly(
bool value =
true);
1277 Benchmark* Complexity(BigO complexity = benchmark::oAuto);
1281 Benchmark* Complexity(BigOFunc* complexity);
1284 Benchmark* ComputeStatistics(
const std::string& name,
1285 StatisticsFunc* statistics,
1286 StatisticUnit unit = kTime);
1307 Benchmark* ThreadRange(
int min_threads,
int max_threads);
1313 Benchmark* DenseThreadRange(
int min_threads,
int max_threads,
int stride = 1);
1318 virtual void Run(
State& state) = 0;
1320 TimeUnit GetTimeUnit()
const;
1323 explicit Benchmark(
const std::string& name);
1324 void SetName(
const std::string& name);
1327 const char* GetName()
const;
1328 int ArgsCnt()
const;
1329 const char* GetArgName(
int arg)
const;
1336 AggregationReportMode aggregation_report_mode_;
1337 std::vector<std::string> arg_names_;
1338 std::vector<std::vector<int64_t> > args_;
1340 TimeUnit time_unit_;
1341 bool use_default_time_unit_;
1343 int range_multiplier_;
1345 double min_warmup_time_;
1346 IterationCount iterations_;
1348 bool measure_process_cpu_time_;
1349 bool use_real_time_;
1350 bool use_manual_time_;
1352 BigOFunc* complexity_lambda_;
1353 std::vector<Statistics> statistics_;
1354 std::vector<int> thread_counts_;
1357 callback_function setup_;
1358 callback_function teardown_;
1361#if defined(BENCHMARK_HAS_CXX11)
1367#if defined(BENCHMARK_HAS_CXX11)
1380 internal::Function* fn);
1382#if defined(BENCHMARK_HAS_CXX11)
1383template <
class Lambda>
1389BENCHMARK_EXPORT
void ClearRegisteredBenchmarks();
1399 void Run(
State& st) BENCHMARK_OVERRIDE;
1405#ifdef BENCHMARK_HAS_CXX11
1406template <
class Lambda>
1407class LambdaBenchmark :
public Benchmark {
1409 void Run(
State& st) BENCHMARK_OVERRIDE { lambda_(st); }
1412 template <
class OLambda>
1413 LambdaBenchmark(
const std::string& name, OLambda&& lam)
1414 : Benchmark(name), lambda_(std::forward<OLambda>(lam)) {}
1416 LambdaBenchmark(LambdaBenchmark
const&) =
delete;
1418 template <
class Lam>
1419 friend Benchmark* ::benchmark::RegisterBenchmark(
const std::string&, Lam&&);
1426inline internal::Benchmark* RegisterBenchmark(
const std::string& name,
1427 internal::Function* fn) {
1430 return internal::RegisterBenchmarkInternal(
1431 ::new internal::FunctionBenchmark(name, fn));
1434#ifdef BENCHMARK_HAS_CXX11
1435template <
class Lambda>
1436internal::Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn) {
1438 internal::LambdaBenchmark<typename std::decay<Lambda>::type>;
1441 return internal::RegisterBenchmarkInternal(
1442 ::new BenchType(name, std::forward<Lambda>(fn)));
1446#if defined(BENCHMARK_HAS_CXX11) && \
1447 (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
1448template <
class Lambda,
class... Args>
1449internal::Benchmark* RegisterBenchmark(
const std::string& name, Lambda&& fn,
1451 return benchmark::RegisterBenchmark(
1455#define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
1463 void Run(
State& st) BENCHMARK_OVERRIDE {
1465 this->BenchmarkCase(st);
1470 virtual void SetUp(
const State&) {}
1471 virtual void TearDown(
const State&) {}
1473 virtual void SetUp(
State& st) { SetUp(
const_cast<const State&
>(st)); }
1474 virtual void TearDown(
State& st) { TearDown(
const_cast<const State&
>(st)); }
1477 virtual void BenchmarkCase(
State&) = 0;
1487#if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
1488#define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
1490#define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
1494#ifdef BENCHMARK_HAS_CXX11
1495#define BENCHMARK_PRIVATE_NAME(...) \
1496 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, \
1499#define BENCHMARK_PRIVATE_NAME(n) \
1500 BENCHMARK_PRIVATE_CONCAT(benchmark_uniq_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
1503#define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
1504#define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
1506#define BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method) \
1507 BaseClass##_##Method##_Benchmark
1509#define BENCHMARK_PRIVATE_DECLARE(n) \
1510 static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
1513#ifdef BENCHMARK_HAS_CXX11
1514#define BENCHMARK(...) \
1515 BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1516 (::benchmark::internal::RegisterBenchmarkInternal( \
1517 new ::benchmark::internal::FunctionBenchmark(#__VA_ARGS__, \
1520#define BENCHMARK(n) \
1521 BENCHMARK_PRIVATE_DECLARE(n) = \
1522 (::benchmark::internal::RegisterBenchmarkInternal( \
1523 new ::benchmark::internal::FunctionBenchmark(#n, n)))
1527#define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
1528#define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
1529#define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
1530#define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
1531#define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
1532 BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
1534#ifdef BENCHMARK_HAS_CXX11
1547#define BENCHMARK_CAPTURE(func, test_case_name, ...) \
1548 BENCHMARK_PRIVATE_DECLARE(_benchmark_) = \
1549 (::benchmark::internal::RegisterBenchmarkInternal( \
1550 new ::benchmark::internal::FunctionBenchmark( \
1551 #func "/" #test_case_name, \
1552 [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
1564#define BENCHMARK_TEMPLATE1(n, a) \
1565 BENCHMARK_PRIVATE_DECLARE(n) = \
1566 (::benchmark::internal::RegisterBenchmarkInternal( \
1567 new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
1569#define BENCHMARK_TEMPLATE2(n, a, b) \
1570 BENCHMARK_PRIVATE_DECLARE(n) = \
1571 (::benchmark::internal::RegisterBenchmarkInternal( \
1572 new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
1575#ifdef BENCHMARK_HAS_CXX11
1576#define BENCHMARK_TEMPLATE(n, ...) \
1577 BENCHMARK_PRIVATE_DECLARE(n) = \
1578 (::benchmark::internal::RegisterBenchmarkInternal( \
1579 new ::benchmark::internal::FunctionBenchmark( \
1580 #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
1582#define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
1585#ifdef BENCHMARK_HAS_CXX11
1598#define BENCHMARK_TEMPLATE1_CAPTURE(func, a, test_case_name, ...) \
1599 BENCHMARK_CAPTURE(func<a>, test_case_name, __VA_ARGS__)
1601#define BENCHMARK_TEMPLATE2_CAPTURE(func, a, b, test_case_name, ...) \
1602 BENCHMARK_PRIVATE_DECLARE(func) = \
1603 (::benchmark::internal::RegisterBenchmarkInternal( \
1604 new ::benchmark::internal::FunctionBenchmark( \
1605 #func "<" #a "," #b ">" \
1606 "/" #test_case_name, \
1607 [](::benchmark::State& st) { func<a, b>(st, __VA_ARGS__); })))
1610#define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1611 class BaseClass##_##Method##_Benchmark : public BaseClass { \
1613 BaseClass##_##Method##_Benchmark() { \
1614 this->SetName(#BaseClass "/" #Method); \
1618 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1621#define BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1622 class BaseClass##_##Method##_Benchmark : public BaseClass<a> { \
1624 BaseClass##_##Method##_Benchmark() { \
1625 this->SetName(#BaseClass "<" #a ">/" #Method); \
1629 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1632#define BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1633 class BaseClass##_##Method##_Benchmark : public BaseClass<a, b> { \
1635 BaseClass##_##Method##_Benchmark() { \
1636 this->SetName(#BaseClass "<" #a "," #b ">/" #Method); \
1640 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1643#ifdef BENCHMARK_HAS_CXX11
1644#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, ...) \
1645 class BaseClass##_##Method##_Benchmark : public BaseClass<__VA_ARGS__> { \
1647 BaseClass##_##Method##_Benchmark() { \
1648 this->SetName(#BaseClass "<" #__VA_ARGS__ ">/" #Method); \
1652 void BenchmarkCase(::benchmark::State&) BENCHMARK_OVERRIDE; \
1655#define BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(n, a) \
1656 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(n, a)
1659#define BENCHMARK_DEFINE_F(BaseClass, Method) \
1660 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1661 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1663#define BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a) \
1664 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1665 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1667#define BENCHMARK_TEMPLATE2_DEFINE_F(BaseClass, Method, a, b) \
1668 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1669 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1671#ifdef BENCHMARK_HAS_CXX11
1672#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, ...) \
1673 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1674 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1676#define BENCHMARK_TEMPLATE_DEFINE_F(BaseClass, Method, a) \
1677 BENCHMARK_TEMPLATE1_DEFINE_F(BaseClass, Method, a)
1680#define BENCHMARK_REGISTER_F(BaseClass, Method) \
1681 BENCHMARK_PRIVATE_REGISTER_F(BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method))
1683#define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
1684 BENCHMARK_PRIVATE_DECLARE(TestName) = \
1685 (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
1688#define BENCHMARK_F(BaseClass, Method) \
1689 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
1690 BENCHMARK_REGISTER_F(BaseClass, Method); \
1691 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1693#define BENCHMARK_TEMPLATE1_F(BaseClass, Method, a) \
1694 BENCHMARK_TEMPLATE1_PRIVATE_DECLARE_F(BaseClass, Method, a) \
1695 BENCHMARK_REGISTER_F(BaseClass, Method); \
1696 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1698#define BENCHMARK_TEMPLATE2_F(BaseClass, Method, a, b) \
1699 BENCHMARK_TEMPLATE2_PRIVATE_DECLARE_F(BaseClass, Method, a, b) \
1700 BENCHMARK_REGISTER_F(BaseClass, Method); \
1701 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1703#ifdef BENCHMARK_HAS_CXX11
1704#define BENCHMARK_TEMPLATE_F(BaseClass, Method, ...) \
1705 BENCHMARK_TEMPLATE_PRIVATE_DECLARE_F(BaseClass, Method, __VA_ARGS__) \
1706 BENCHMARK_REGISTER_F(BaseClass, Method); \
1707 void BENCHMARK_PRIVATE_CONCAT_NAME(BaseClass, Method)::BenchmarkCase
1709#define BENCHMARK_TEMPLATE_F(BaseClass, Method, a) \
1710 BENCHMARK_TEMPLATE1_F(BaseClass, Method, a)
1715#define BENCHMARK_MAIN() \
1716 int main(int argc, char** argv) { \
1717 char arg0_default[] = "benchmark"; \
1718 char* args_default = arg0_default; \
1721 argv = &args_default; \
1723 ::benchmark::Initialize(&argc, argv); \
1724 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
1725 ::benchmark::RunSpecifiedBenchmarks(); \
1726 ::benchmark::Shutdown(); \
1729 int main(int, char**)
1734namespace benchmark {
1744 enum Scaling { UNKNOWN, ENABLED, DISABLED };
1748 double cycles_per_second;
1749 std::vector<CacheInfo> caches;
1750 std::vector<double> load_avg;
1756 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
CPUInfo);
1766 BENCHMARK_DISALLOW_COPY_AND_ASSIGN(
SystemInfo);
1773 std::string function_name;
1775 std::string min_time;
1776 std::string min_warmup_time;
1777 std::string iterations;
1778 std::string repetitions;
1779 std::string time_type;
1780 std::string threads;
1784 std::string str()
const;
1798 size_t name_field_width;
1799 static const char* executable_name;
1804 static const int64_t no_repetition_index = -1;
1805 enum RunType { RT_Iteration, RT_Aggregate };
1808 : run_type(RT_Iteration),
1809 aggregate_unit(kTime),
1810 skipped(internal::NotSkipped),
1813 time_unit(GetDefaultTimeUnit()),
1814 real_accumulated_time(0),
1815 cpu_accumulated_time(0),
1816 max_heapbytes_used(0),
1817 use_real_time_for_initial_big_o(
false),
1819 complexity_lambda(),
1821 report_big_o(
false),
1823 memory_result(NULL),
1824 allocs_per_iter(0.0) {}
1826 std::string benchmark_name()
const;
1828 int64_t family_index;
1829 int64_t per_family_instance_index;
1831 std::string aggregate_name;
1832 StatisticUnit aggregate_unit;
1833 std::string report_label;
1834 internal::Skipped skipped;
1835 std::string skip_message;
1837 IterationCount iterations;
1839 int64_t repetition_index;
1840 int64_t repetitions;
1842 double real_accumulated_time;
1843 double cpu_accumulated_time;
1849 double GetAdjustedRealTime()
const;
1855 double GetAdjustedCPUTime()
const;
1858 double max_heapbytes_used;
1862 bool use_real_time_for_initial_big_o;
1866 BigOFunc* complexity_lambda;
1867 ComplexityN complexity_n;
1870 const std::vector<internal::Statistics>* statistics;
1876 UserCounters counters;
1880 double allocs_per_iter;
1893 std::vector<BenchmarkReporter::Run> Runs;
1906 virtual bool ReportContext(
const Context& context) = 0;
1910 virtual void ReportRunsConfig(
double ,
1921 virtual void ReportRuns(
const std::vector<Run>& report) = 0;
1925 virtual void Finalize() {}
1929 void SetOutputStream(std::ostream* out) {
1931 output_stream_ = out;
1936 void SetErrorStream(std::ostream* err) {
1938 error_stream_ = err;
1941 std::ostream& GetOutputStream()
const {
return *output_stream_; }
1943 std::ostream& GetErrorStream()
const {
return *error_stream_; }
1945 virtual ~BenchmarkReporter();
1950 static void PrintBasicContext(std::ostream* out, Context
const& context);
1953 std::ostream* output_stream_;
1954 std::ostream* error_stream_;
1961 enum OutputOptions {
1965 OO_ColorTabular = OO_Color | OO_Tabular,
1966 OO_Defaults = OO_ColorTabular
1969 : output_options_(opts_), name_field_width_(0), printed_header_(
false) {}
1971 bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1972 void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1975 virtual void PrintRunData(
const Run& report);
1976 virtual void PrintHeader(
const Run& report);
1978 OutputOptions output_options_;
1979 size_t name_field_width_;
1980 UserCounters prev_counters_;
1981 bool printed_header_;
1987 bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
1988 void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
1989 void Finalize() BENCHMARK_OVERRIDE;
1992 void PrintRunData(
const Run& report);
1997class BENCHMARK_EXPORT BENCHMARK_DEPRECATED_MSG(
1998 "The CSV Reporter will be removed in a future release") CSVReporter
2001 CSVReporter() : printed_header_(false) {}
2002 bool ReportContext(
const Context& context) BENCHMARK_OVERRIDE;
2003 void ReportRuns(
const std::vector<Run>& reports) BENCHMARK_OVERRIDE;
2006 void PrintRunData(
const Run& report);
2008 bool printed_header_;
2009 std::set<std::string> user_counter_names_;
2012inline const char* GetTimeUnitString(TimeUnit unit) {
2023 BENCHMARK_UNREACHABLE();
2026inline double GetTimeUnitMultiplier(TimeUnit unit) {
2037 BENCHMARK_UNREACHABLE();
2050std::vector<int64_t> CreateRange(int64_t lo, int64_t hi,
int multi);
2054std::vector<int64_t> CreateDenseRange(int64_t start, int64_t limit,
int step);
2058#if defined(_MSC_VER)
Definition: benchmark.h:1792
Definition: benchmark.h:1959
Definition: benchmark.h:633
Definition: benchmark.h:1459
Definition: benchmark.h:1984
Definition: benchmark.h:378
Definition: benchmark.h:422
Definition: benchmark.h:762
Definition: benchmark_register.cc:73
Definition: benchmark_api_internal.h:18
Definition: benchmark.h:1122
Definition: benchmark.h:1394
Definition: perf_counters.h:149
Definition: thread_manager.h:12
Definition: thread_timer.h:10
Definition: benchmark.h:1772
Definition: benchmark.h:1794
Definition: benchmark.h:1883
Definition: benchmark.h:1803
Definition: benchmark.h:1737
Definition: benchmark.h:1736
Definition: benchmark.h:382
Definition: benchmark.h:1065
Definition: benchmark.h:1064
Definition: benchmark.h:1760
Definition: benchmark.h:713