1 #include "benchmark/benchmark.h"
22 #define BENCHMARK_NOINLINE __attribute__((noinline))
24 #define BENCHMARK_NOINLINE
33 double CalculatePi(
int depth) {
36 double numerator =
static_cast<double>(((
i % 2) * 2) - 1);
37 double denominator =
static_cast<double>((2 *
i) - 1);
38 pi += numerator / denominator;
40 return (pi - 1.0) * 4;
43 std::set<int> ConstructRandomSet(
int size) {
45 for (
int i = 0;
i <
size; ++
i) s.insert(
i);
60 state.SetLabel(ss.str());
67 while (
state.KeepRunning()) pi = CalculatePi(
state.range(0));
70 state.SetLabel(ss.str());
75 static const int depth = 1024;
76 while (
state.KeepRunning()) {
85 while (
state.KeepRunning()) {
87 std::set<int>
data = ConstructRandomSet(
state.range(0));
89 for (
int j = 0; j <
state.range(1); ++j)
data.insert(rand());
92 state.SetBytesProcessed(
state.iterations() *
state.range(1) *
sizeof(
int));
96 template <
typename Container,
100 while (
state.KeepRunning()) {
102 for (
int i =
state.range(0); --
i;)
c.push_back(
v);
104 const size_t items_processed =
state.iterations() *
state.range(0);
105 state.SetItemsProcessed(items_processed);
106 state.SetBytesProcessed(items_processed *
sizeof(
v));
109 ->Range(1 << 0, 1 << 10);
112 #if __cplusplus >= 201103L
124 if (
state.thread_index == 0) {
129 while (
state.KeepRunning()) {
130 std::lock_guard<std::mutex>
l(test_vector_mu);
137 if (
state.thread_index == 0) {
145 while (
state.KeepRunning()) {
146 for (
int i = 0;
i <
state.range(0); ++
i)
153 int size =
state.range(0) /
static_cast<int>(
sizeof(
int));
155 int from = thread_size *
state.thread_index;
156 int to =
from + thread_size;
158 if (
state.thread_index == 0) {
162 while (
state.KeepRunning()) {
170 if (
state.thread_index == 0) {
177 size_t slept_for = 0;
178 int microseconds =
state.range(0);
179 std::chrono::duration<double, std::micro> sleep_duration{
180 static_cast<double>(microseconds)};
182 while (
state.KeepRunning()) {
185 std::this_thread::sleep_for(
186 std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
190 std::chrono::duration_cast<std::chrono::duration<double>>(
end -
start);
192 state.SetIterationTime(elapsed.count());
193 slept_for += microseconds;
195 state.SetItemsProcessed(slept_for);
200 #if __cplusplus >= 201103L
202 template <
class...
Args>
204 while (
state.KeepRunning()) {
207 BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44);
208 BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test,
std::string(
"abc"),
209 std::pair<int, double>(42, 3.8));
212 while(
state.KeepRunning()) {}
214 BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
216 #endif // __cplusplus >= 201103L
219 switch (st.
range(0)) {
231 assert(
false &&
"Invalid test case number");