1 
2 #include "benchmark/benchmark.h"
3 
4 #define BASIC_BENCHMARK_TEST(x) BENCHMARK(x)->Arg(8)->Arg(512)->Arg(8192)
5 
BM_empty(benchmark::State & state)6 void BM_empty(benchmark::State& state) {
7   for (auto _ : state) {
8     benchmark::DoNotOptimize(state.iterations());
9   }
10 }
11 BENCHMARK(BM_empty);
12 BENCHMARK(BM_empty)->ThreadPerCpu();
13 
BM_spin_empty(benchmark::State & state)14 void BM_spin_empty(benchmark::State& state) {
15   for (auto _ : state) {
16     for (int x = 0; x < state.range(0); ++x) {
17       benchmark::DoNotOptimize(x);
18     }
19   }
20 }
21 BASIC_BENCHMARK_TEST(BM_spin_empty);
22 BASIC_BENCHMARK_TEST(BM_spin_empty)->ThreadPerCpu();
23 
BM_spin_pause_before(benchmark::State & state)24 void BM_spin_pause_before(benchmark::State& state) {
25   for (int i = 0; i < state.range(0); ++i) {
26     benchmark::DoNotOptimize(i);
27   }
28   for (auto _ : state) {
29     for (int i = 0; i < state.range(0); ++i) {
30       benchmark::DoNotOptimize(i);
31     }
32   }
33 }
34 BASIC_BENCHMARK_TEST(BM_spin_pause_before);
35 BASIC_BENCHMARK_TEST(BM_spin_pause_before)->ThreadPerCpu();
36 
BM_spin_pause_during(benchmark::State & state)37 void BM_spin_pause_during(benchmark::State& state) {
38   for (auto _ : state) {
39     state.PauseTiming();
40     for (int i = 0; i < state.range(0); ++i) {
41       benchmark::DoNotOptimize(i);
42     }
43     state.ResumeTiming();
44     for (int i = 0; i < state.range(0); ++i) {
45       benchmark::DoNotOptimize(i);
46     }
47   }
48 }
49 BASIC_BENCHMARK_TEST(BM_spin_pause_during);
50 BASIC_BENCHMARK_TEST(BM_spin_pause_during)->ThreadPerCpu();
51 
BM_pause_during(benchmark::State & state)52 void BM_pause_during(benchmark::State& state) {
53   for (auto _ : state) {
54     state.PauseTiming();
55     state.ResumeTiming();
56   }
57 }
58 BENCHMARK(BM_pause_during);
59 BENCHMARK(BM_pause_during)->ThreadPerCpu();
60 BENCHMARK(BM_pause_during)->UseRealTime();
61 BENCHMARK(BM_pause_during)->UseRealTime()->ThreadPerCpu();
62 
BM_spin_pause_after(benchmark::State & state)63 void BM_spin_pause_after(benchmark::State& state) {
64   for (auto _ : state) {
65     for (int i = 0; i < state.range(0); ++i) {
66       benchmark::DoNotOptimize(i);
67     }
68   }
69   for (int i = 0; i < state.range(0); ++i) {
70     benchmark::DoNotOptimize(i);
71   }
72 }
73 BASIC_BENCHMARK_TEST(BM_spin_pause_after);
74 BASIC_BENCHMARK_TEST(BM_spin_pause_after)->ThreadPerCpu();
75 
BM_spin_pause_before_and_after(benchmark::State & state)76 void BM_spin_pause_before_and_after(benchmark::State& state) {
77   for (int i = 0; i < state.range(0); ++i) {
78     benchmark::DoNotOptimize(i);
79   }
80   for (auto _ : state) {
81     for (int i = 0; i < state.range(0); ++i) {
82       benchmark::DoNotOptimize(i);
83     }
84   }
85   for (int i = 0; i < state.range(0); ++i) {
86     benchmark::DoNotOptimize(i);
87   }
88 }
89 BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after);
90 BASIC_BENCHMARK_TEST(BM_spin_pause_before_and_after)->ThreadPerCpu();
91 
BM_empty_stop_start(benchmark::State & state)92 void BM_empty_stop_start(benchmark::State& state) {
93   for (auto _ : state) {
94   }
95 }
96 BENCHMARK(BM_empty_stop_start);
97 BENCHMARK(BM_empty_stop_start)->ThreadPerCpu();
98 
99 
BM_KeepRunning(benchmark::State & state)100 void BM_KeepRunning(benchmark::State& state) {
101   size_t iter_count = 0;
102   assert(iter_count == state.iterations());
103   while (state.KeepRunning()) {
104     ++iter_count;
105   }
106   assert(iter_count == state.iterations());
107 }
108 BENCHMARK(BM_KeepRunning);
109 
BM_KeepRunningBatch(benchmark::State & state)110 void BM_KeepRunningBatch(benchmark::State& state) {
111   // Choose a prime batch size to avoid evenly dividing max_iterations.
112   const size_t batch_size = 101;
113   size_t iter_count = 0;
114   while (state.KeepRunningBatch(batch_size)) {
115     iter_count += batch_size;
116   }
117   assert(state.iterations() == iter_count);
118 }
119 BENCHMARK(BM_KeepRunningBatch);
120 
BM_RangedFor(benchmark::State & state)121 void BM_RangedFor(benchmark::State& state) {
122   size_t iter_count = 0;
123   for (auto _ : state) {
124     ++iter_count;
125   }
126   assert(iter_count == state.max_iterations);
127 }
128 BENCHMARK(BM_RangedFor);
129 
130 // Ensure that StateIterator provides all the necessary typedefs required to
131 // instantiate std::iterator_traits.
132 static_assert(std::is_same<
133   typename std::iterator_traits<benchmark::State::StateIterator>::value_type,
134   typename benchmark::State::StateIterator::value_type>::value, "");
135 
136 BENCHMARK_MAIN();
137