123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- #ifndef BENCHMARK_BENCHMARK_API_H_
- #define BENCHMARK_BENCHMARK_API_H_
- #include <assert.h>
- #include <stddef.h>
- #include <stdint.h>
- #include "macros.h"
- namespace benchmark {
- class BenchmarkReporter;
- void Initialize(int* argc, const char** argv);
- void RunSpecifiedBenchmarks();
- void RunSpecifiedBenchmarks(BenchmarkReporter* reporter);
- namespace internal {
- class Benchmark;
- class BenchmarkImp;
- template <class T> struct Voider {
- typedef void type;
- };
- template <class T, class = void>
- struct EnableIfString {};
- template <class T>
- struct EnableIfString<T, typename Voider<typename T::basic_string>::type> {
- typedef int type;
- };
- void UseCharPointer(char const volatile*);
- }
- #if defined(__clang__) && defined(__GNUC__)
- template <class Tp>
- inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
- asm volatile("" : "+m" (const_cast<Tp&>(value)));
- }
- #elif defined(__GNUC__)
- template <class Tp>
- inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
- asm volatile("" : "+rm" (const_cast<Tp&>(value)));
- }
- #else
- template <class Tp>
- inline BENCHMARK_ALWAYS_INLINE void DoNotOptimize(Tp const& value) {
- internal::UseCharPointer(&reinterpret_cast<char const volatile&>(value));
- }
- #endif
- class State {
- public:
- State(size_t max_iters, bool has_x, int x, bool has_y, int y, int thread_i);
-
-
-
- bool KeepRunning() {
- if (BENCHMARK_BUILTIN_EXPECT(!started_, false)) {
- ResumeTiming();
- started_ = true;
- }
- bool const res = total_iterations_++ < max_iterations;
- if (BENCHMARK_BUILTIN_EXPECT(!res, false)) {
- assert(started_);
- PauseTiming();
-
- total_iterations_ = max_iterations;
- }
- return res;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- void PauseTiming();
-
-
-
-
-
-
-
-
-
-
-
-
- void ResumeTiming();
-
-
-
-
-
-
-
- BENCHMARK_ALWAYS_INLINE
- void SetBytesProcessed(size_t bytes) {
- bytes_processed_ = bytes;
- }
- BENCHMARK_ALWAYS_INLINE
- size_t bytes_processed() const {
- return bytes_processed_;
- }
-
-
-
-
-
-
- BENCHMARK_ALWAYS_INLINE
- void SetItemsProcessed(size_t items) {
- items_processed_ = items;
- }
- BENCHMARK_ALWAYS_INLINE
- size_t items_processed() const {
- return items_processed_;
- }
-
-
-
-
-
-
-
-
-
-
-
-
- void SetLabel(const char* label);
-
-
-
-
- template <class StringType>
- void SetLabel(StringType const & str,
- typename internal::EnableIfString<StringType>::type = 1) {
- this->SetLabel(str.c_str());
- }
-
- BENCHMARK_ALWAYS_INLINE
- int range_x() const {
- assert(has_range_x_);
- ((void)has_range_x_);
- return range_x_;
- }
- BENCHMARK_ALWAYS_INLINE
- int range_y() const {
- assert(has_range_y_);
- ((void)has_range_y_);
- return range_y_;
- }
- BENCHMARK_ALWAYS_INLINE
- size_t iterations() const { return total_iterations_; }
- private:
- bool started_;
- size_t total_iterations_;
- bool has_range_x_;
- int range_x_;
- bool has_range_y_;
- int range_y_;
- size_t bytes_processed_;
- size_t items_processed_;
- public:
- const int thread_index;
- const size_t max_iterations;
- private:
- BENCHMARK_DISALLOW_COPY_AND_ASSIGN(State);
- };
- namespace internal {
- typedef void(Function)(State&);
- class Benchmark {
- public:
- Benchmark(const char* name, Function* f);
- ~Benchmark();
-
-
-
-
-
- Benchmark* Arg(int x);
-
-
-
- Benchmark* Range(int start, int limit);
-
-
- Benchmark* DenseRange(int start, int limit);
-
-
-
- Benchmark* ArgPair(int x, int y);
-
-
-
-
-
- Benchmark* RangePair(int lo1, int hi1, int lo2, int hi2);
-
-
-
- Benchmark* Apply(void (*func)(Benchmark* benchmark));
-
-
- Benchmark* MinTime(double t);
-
-
-
-
-
- Benchmark* UseRealTime();
-
-
-
-
- Benchmark* Threads(int t);
-
-
-
-
-
-
-
-
-
-
-
-
- Benchmark* ThreadRange(int min_threads, int max_threads);
-
- Benchmark* ThreadPerCpu();
-
- struct Instance;
- private:
- BenchmarkImp* imp_;
- BENCHMARK_DISALLOW_COPY_AND_ASSIGN(Benchmark);
- };
- }
- }
- #if defined(__COUNTER__) && (__COUNTER__ + 1 == __COUNTER__ + 0)
- #define BENCHMARK_PRIVATE_UNIQUE_ID __COUNTER__
- #else
- #define BENCHMARK_PRIVATE_UNIQUE_ID __LINE__
- #endif
- #define BENCHMARK_PRIVATE_NAME(n) \
- BENCHMARK_PRIVATE_CONCAT(_benchmark_, BENCHMARK_PRIVATE_UNIQUE_ID, n)
- #define BENCHMARK_PRIVATE_CONCAT(a, b, c) BENCHMARK_PRIVATE_CONCAT2(a, b, c)
- #define BENCHMARK_PRIVATE_CONCAT2(a, b, c) a##b##c
- #define BENCHMARK_PRIVATE_DECLARE(n) \
- static ::benchmark::internal::Benchmark* \
- BENCHMARK_PRIVATE_NAME(n) BENCHMARK_UNUSED
- #define BENCHMARK(n) \
- BENCHMARK_PRIVATE_DECLARE(n) = (new ::benchmark::internal::Benchmark(#n, n))
- #define BENCHMARK_WITH_ARG(n, a) BENCHMARK(n)->Arg((a))
- #define BENCHMARK_WITH_ARG2(n, a1, a2) BENCHMARK(n)->ArgPair((a1), (a2))
- #define BENCHMARK_RANGE(n, lo, hi) BENCHMARK(n)->Range((lo), (hi))
- #define BENCHMARK_RANGE2(n, l1, h1, l2, h2) \
- BENCHMARK(n)->RangePair((l1), (h1), (l2), (h2))
- #define BENCHMARK_TEMPLATE1(n, a) \
- BENCHMARK_PRIVATE_DECLARE(n) = \
- (new ::benchmark::internal::Benchmark(#n "<" #a ">", n<a>))
- #define BENCHMARK_TEMPLATE2(n, a, b) \
- BENCHMARK_PRIVATE_DECLARE(n) = \
- (new ::benchmark::internal::Benchmark(#n "<" #a "," #b ">", n<a, b>))
- #if __cplusplus >= 201103L
- #define BENCHMARK_TEMPLATE(n, ...) \
- BENCHMARK_PRIVATE_DECLARE(n) = \
- (new ::benchmark::internal::Benchmark( \
- #n "<" #__VA_ARGS__ ">", n<__VA_ARGS__>))
- #else
- #define BENCHMARK_TEMPLATE(n, a) BENCHMARK_TEMPLATE1(n, a)
- #endif
- #define BENCHMARK_MAIN() \
- int main(int argc, const char** argv) { \
- ::benchmark::Initialize(&argc, argv); \
- ::benchmark::RunSpecifiedBenchmarks(); \
- }
- #endif
|