Go to the documentation of this file.
163 #ifndef BENCHMARK_BENCHMARK_H_
164 #define BENCHMARK_BENCHMARK_H_
167 #if __cplusplus >= 201103L
168 #define BENCHMARK_HAS_CXX11
181 #if defined(BENCHMARK_HAS_CXX11)
182 #include <type_traits>
183 #include <initializer_list>
187 #if defined(_MSC_VER)
191 #ifndef BENCHMARK_HAS_CXX11
192 #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
193 TypeName(const TypeName&); \
194 TypeName& operator=(const TypeName&)
196 #define BENCHMARK_DISALLOW_COPY_AND_ASSIGN(TypeName) \
197 TypeName(const TypeName&) = delete; \
198 TypeName& operator=(const TypeName&) = delete
201 #if defined(__GNUC__)
202 #define BENCHMARK_UNUSED __attribute__((unused))
203 #define BENCHMARK_ALWAYS_INLINE __attribute__((always_inline))
204 #define BENCHMARK_NOEXCEPT noexcept
205 #define BENCHMARK_NOEXCEPT_OP(x) noexcept(x)
206 #elif defined(_MSC_VER) && !defined(__clang__)
207 #define BENCHMARK_UNUSED
208 #define BENCHMARK_ALWAYS_INLINE __forceinline
210 #define BENCHMARK_NOEXCEPT noexcept
211 #define BENCHMARK_NOEXCEPT_OP(x) noexcept(x)
213 #define BENCHMARK_NOEXCEPT
214 #define BENCHMARK_NOEXCEPT_OP(x)
216 #define __func__ __FUNCTION__
218 #define BENCHMARK_UNUSED
219 #define BENCHMARK_ALWAYS_INLINE
220 #define BENCHMARK_NOEXCEPT
221 #define BENCHMARK_NOEXCEPT_OP(x)
224 #define BENCHMARK_INTERNAL_TOSTRING2(x) #x
225 #define BENCHMARK_INTERNAL_TOSTRING(x) BENCHMARK_INTERNAL_TOSTRING2(x)
227 #if defined(__GNUC__)
228 #define BENCHMARK_BUILTIN_EXPECT(x, y) __builtin_expect(x, y)
229 #define BENCHMARK_DEPRECATED_MSG(msg) __attribute__((deprecated(msg)))
231 #define BENCHMARK_BUILTIN_EXPECT(x, y) x
232 #define BENCHMARK_DEPRECATED_MSG(msg)
233 #define BENCHMARK_WARNING_MSG(msg) __pragma(message(__FILE__ "(" BENCHMARK_INTERNAL_TOSTRING(__LINE__) ") : warning note: " msg))
236 #if defined(__GNUC__) && !defined(__clang__)
237 #define BENCHMARK_GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__)
242 class BenchmarkReporter;
265 BenchmarkReporter* file_reporter);
277 class BenchmarkFamilies;
292 #if !defined(__GNUC__) || defined(__pnacl__) || defined(EMSCRIPTN)
293 # define BENCHMARK_HAS_NO_INLINE_ASSEMBLY
300 #ifndef BENCHMARK_HAS_NO_INLINE_ASSEMBLY
305 #if defined(__clang__)
306 asm volatile(
"" : :
"g"(
value) :
"memory");
308 asm volatile(
"" : :
"i,r,m"(
value) :
"memory");
314 asm volatile(
"" : : :
"memory");
316 #elif defined(_MSC_VER)
386 #if defined(BENCHMARK_HAS_CXX11)
561 State(
size_t max_iters,
const std::vector<int>& ranges,
int thread_i,
617 std::vector<int>
args;
639 std::vector<std::pair<int, int> > ranges;
640 ranges.push_back(std::make_pair(lo1, hi1));
641 ranges.push_back(std::make_pair(lo2, hi2));
743 static void AddRange(std::vector<int>*
dst,
int lo,
int hi,
int mult);
751 std::vector<std::vector<int> >
args_;
775 #if defined(BENCHMARK_HAS_CXX11)
776 template <
class Lambda>
787 class FunctionBenchmark :
public Benchmark {
798 #ifdef BENCHMARK_HAS_CXX11
799 template <
class Lambda>
800 class LambdaBenchmark :
public Benchmark {
802 virtual void Run(
State& st) { lambda_(st); }
805 template <
class OLambda>
806 LambdaBenchmark(
const char*
name, OLambda&& lam)
809 LambdaBenchmark(LambdaBenchmark
const&) =
delete;
824 ::
new internal::FunctionBenchmark(
name,
fn));
827 #ifdef BENCHMARK_HAS_CXX11
828 template <
class Lambda>
833 ::
new BenchType(
name, std::forward<Lambda>(
fn)));
837 #if defined(BENCHMARK_HAS_CXX11) && \
838 (!defined(BENCHMARK_GCC_VERSION) || BENCHMARK_GCC_VERSION >= 409)
839 template <
class Lambda,
class...
Args>
846 #define BENCHMARK_HAS_NO_VARIADIC_REGISTER_BENCHMARK
879 #if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
880 #define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
882 #define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
886 #define BENCHMARK_PRIVATE_NAME(n) \
887 BENCHMARK_PRIVATE_CONCAT(_benchmark_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
888 #define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
889 #define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
891 #define BENCHMARK_PRIVATE_DECLARE(n) \
892 static ::benchmark::internal::Benchmark* BENCHMARK_PRIVATE_NAME(n) \
895 #define BENCHMARK(n) \
896 BENCHMARK_PRIVATE_DECLARE(n) = \
897 (::benchmark::internal::RegisterBenchmarkInternal( \
898 new ::benchmark::internal::FunctionBenchmark(#n, n)))
901 #define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
902 #define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->Args({(a1), (a2)})
903 #define BENCHMARK_WITH_UNIT(n, t) BENCHMARK(n)->Unit((t))
904 #define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
905 #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
906 BENCHMARK(n)->RangePair({{(l1), (h1)}, {(l2), (h2)}})
908 #if __cplusplus >= 201103L
921 #define BENCHMARK_CAPTURE(func, test_case_name, ...) \
922 BENCHMARK_PRIVATE_DECLARE(func) = \
923 (::benchmark::internal::RegisterBenchmarkInternal( \
924 new ::benchmark::internal::FunctionBenchmark( \
925 #func "/" #test_case_name, \
926 [](::benchmark::State& st) { func(st, __VA_ARGS__); })))
928 #endif // __cplusplus >= 11
938 #define BENCHMARK_TEMPLATE1(n, a) \
939 BENCHMARK_PRIVATE_DECLARE(n) = \
940 (::benchmark::internal::RegisterBenchmarkInternal( \
941 new ::benchmark::internal::FunctionBenchmark(#n "<" #a ">", n<a>)))
943 #define BENCHMARK_TEMPLATE2(n, a, b) \
944 BENCHMARK_PRIVATE_DECLARE(n) = \
945 (::benchmark::internal::RegisterBenchmarkInternal( \
946 new ::benchmark::internal::FunctionBenchmark(#n "<" #a "," #b ">", \
949 #if __cplusplus >= 201103L
950 #define BENCHMARK_TEMPLATE(n, ...) \
951 BENCHMARK_PRIVATE_DECLARE(n) = \
952 (::benchmark::internal::RegisterBenchmarkInternal( \
953 new ::benchmark::internal::FunctionBenchmark( \
954 #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>)))
956 #define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
959 #define BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
960 class BaseClass##_##Method##_Benchmark : public BaseClass { \
962 BaseClass##_##Method##_Benchmark() : BaseClass() { \
963 this->SetName(#BaseClass "/" #Method); \
967 virtual void BenchmarkCase(::benchmark::State&); \
970 #define BENCHMARK_DEFINE_F(BaseClass, Method) \
971 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
972 void BaseClass##_##Method##_Benchmark::BenchmarkCase
974 #define BENCHMARK_REGISTER_F(BaseClass, Method) \
975 BENCHMARK_PRIVATE_REGISTER_F(BaseClass##_##Method##_Benchmark)
977 #define BENCHMARK_PRIVATE_REGISTER_F(TestName) \
978 BENCHMARK_PRIVATE_DECLARE(TestName) = \
979 (::benchmark::internal::RegisterBenchmarkInternal(new TestName()))
982 #define BENCHMARK_F(BaseClass, Method) \
983 BENCHMARK_PRIVATE_DECLARE_F(BaseClass, Method) \
984 BENCHMARK_REGISTER_F(BaseClass, Method); \
985 void BaseClass##_##Method##_Benchmark::BenchmarkCase
988 #define BENCHMARK_MAIN() \
989 int main(int argc, char** argv) { \
990 ::benchmark::Initialize(&argc, argv); \
991 if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; \
992 ::benchmark::RunSpecifiedBenchmarks(); \
1006 class BenchmarkReporter {
1094 virtual void ReportRuns(
const std::vector<Run>& report) = 0;
1132 class ConsoleReporter :
public BenchmarkReporter {
1146 virtual void ReportRuns(
const std::vector<Run>& reports);
1158 class JSONReporter :
public BenchmarkReporter {
1162 virtual void ReportRuns(
const std::vector<Run>& reports);
1171 class CSVReporter :
public BenchmarkReporter {
1175 virtual void ReportRuns(
const std::vector<Run>& reports);
1210 #endif // BENCHMARK_BENCHMARK_H_
std::ostream & GetErrorStream() const
BigOFunc * complexity_lambda_
std::chrono::duration< std::int_fast64_t > seconds
internal::ThreadManager *const manager_
void SetErrorStream(std::ostream *err)
Benchmark * Iterations(IterationCount n)
void PrintRunData(const Run &report)
const IterationCount max_iterations
Benchmark * ThreadRange(int min_threads, int max_threads)
virtual void SetUp(const State &)
Benchmark * DenseRange(int64_t start, int64_t limit, int step=1)
virtual ~BenchmarkReporter()
OutputOptions output_options_
#define BENCHMARK_DEPRECATED_MSG(msg)
virtual bool ReportContext(const Context &context)
virtual void TearDown(State &st)
void SkipWithError(const char *msg)
Benchmark * Threads(int t)
BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const &value)
void BENCHMARK_ALWAYS_INLINE SetLabel(const std::string &str)
IterationCount total_iterations_
Benchmark * Repetitions(int n)
virtual void Finalize() BENCHMARK_OVERRIDE
Benchmark * Complexity(BigO complexity=benchmark::oAuto)
Benchmark * RangePair(int lo1, int hi1, int lo2, int hi2)
virtual void ReportRuns(const std::vector< Run > &reports) BENCHMARK_OVERRIDE
void SetLabel(const char *label)
Benchmark * Args(const std::vector< int64_t > &args)
internal::Benchmark * RegisterBenchmark(const char *name, internal::Function *fn)
Benchmark * RegisterBenchmarkInternal(Benchmark *)
virtual void PrintHeader(const Run &report)
const char * GetTimeUnitString(TimeUnit unit)
Benchmark * ArgNames(const std::vector< std::string > &names)
void SetName(const char *name)
BENCHMARK_ALWAYS_INLINE size_t items_processed() const
Benchmark * UseManualTime()
UserCounters prev_counters_
double GetAdjustedCPUTime() const
internal::ThreadTimer *const timer_
static void PrintBasicContext(std::ostream *out, Context const &context)
virtual void TearDown(const State &)
BENCHMARK_ALWAYS_INLINE size_t bytes_processed() const
size_t RunSpecifiedBenchmarks()
virtual void BenchmarkCase(State &)=0
virtual bool ReportContext(const Context &context) BENCHMARK_OVERRIDE
Benchmark & operator=(Benchmark const &)
Benchmark * RangeMultiplier(int multiplier)
virtual void PrintRunData(const Run &report)
#define BENCHMARK_ALWAYS_INLINE
Benchmark * Unit(TimeUnit unit)
std::string error_message
static void AddRange(std::vector< int > *dst, int lo, int hi, int mult)
double cpu_accumulated_time
std::ostream * output_stream_
Benchmark * ThreadPerCpu()
BENCHMARK_ALWAYS_INLINE size_t iterations() const
Benchmark * DenseThreadRange(int min_threads, int max_threads, int stride=1)
std::string benchmark_name
void ClearRegisteredBenchmarks()
virtual void Run(State &state)=0
void Initialize(int *argc, char **argv)
std::ostream * error_stream_
Benchmark * Apply(void(*func)(Benchmark *benchmark))
std::vector< std::vector< int64_t > > args_
bool ReportUnrecognizedArguments(int argc, char **argv)
Benchmark * ArgPair(int x, int y)
virtual void SetUp(State &st)
virtual void ReportRuns(const std::vector< Run > &reports) BENCHMARK_OVERRIDE
Benchmark * Arg(int64_t x)
virtual bool ReportContext(const Context &context)=0
BENCHMARK_ALWAYS_INLINE int complexity_length_n()
Benchmark * MinTime(double t)
BENCHMARK_ALWAYS_INLINE Counter(double v=0., Flags f=kDefaults)
virtual void ReportRuns(const std::vector< Run > &report)=0
std::vector< int64_t > range_
std::map< std::string, Counter > UserCounters
Benchmark(const char *name)
std::vector< int > thread_counts_
void SetOutputStream(std::ostream *out)
virtual bool ReportContext(const Context &context) BENCHMARK_OVERRIDE
BENCHMARK_ALWAYS_INLINE int range(std::size_t pos=0) const
#define BENCHMARK_BUILTIN_EXPECT(x, y)
@ RM_ReportAggregatesOnly
double real_accumulated_time
BigOFunc * complexity_lambda
Benchmark * ReportAggregatesOnly(bool value=true)
BENCHMARK_ALWAYS_INLINE void SetItemsProcessed(size_t items)
State(IterationCount max_iters, const std::vector< int64_t > &ranges, int thread_i, int n_threads, internal::ThreadTimer *timer, internal::ThreadManager *manager, internal::PerfCountersMeasurement *perf_counters_measurement)
double GetAdjustedRealTime() const
ConsoleReporter(OutputOptions opts_=OO_Defaults)
IterationCount iterations_
std::set< std::string > user_counter_names_
Benchmark * Range(int64_t start, int64_t limit)
grpc::ClientContext context
std::vector< std::string > arg_names_
Benchmark * ArgName(const std::string &name)
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
double GetTimeUnitMultiplier(TimeUnit unit)
void SetIterationTime(double seconds)
Benchmark * UseRealTime()
Benchmark * Ranges(const std::vector< std::pair< int64_t, int64_t > > &ranges)
BENCHMARK_ALWAYS_INLINE void SetComplexityN(int complexity_n)
FunctionBenchmark(const char *name, Function *func)
static BENCHMARK_UNUSED int stream_init_anchor
def Benchmark(outbase, bench_cpu=True, runs=12, fasttable=False)
std::ostream & GetOutputStream() const
BENCHMARK_DISALLOW_COPY_AND_ASSIGN(State)
void PrintRunData(const Run &report)
virtual void Run(State &st)
virtual void Run(State &st) BENCHMARK_OVERRIDE
BENCHMARK_ALWAYS_INLINE void SetBytesProcessed(size_t bytes)
BENCHMARK_ALWAYS_INLINE int64_t range(std::size_t pos=0) const
IterationCount iterations
void UseCharPointer(char const volatile *)
double() BigOFunc(IterationCount)
double max_heapbytes_used
virtual void ReportRuns(const std::vector< Run > &reports)
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:46