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<int64_t> ConstructRandomSet(
int64_t size) {
45 for (
int i = 0;
i <
size; ++
i) s.insert(s.end(),
i);
60 state.SetLabel(ss.str());
67 for (
auto _ :
state) pi = CalculatePi(
static_cast<int>(
state.range(0)));
70 state.SetLabel(ss.str());
75 static const int depth = 1024;
85 std::set<int64_t>
data;
88 data = ConstructRandomSet(
state.range(0));
90 for (
int j = 0; j <
state.range(1); ++j)
data.insert(rand());
93 state.SetBytesProcessed(
state.iterations() *
state.range(1) *
sizeof(
int));
100 template <
typename Container,
109 state.SetItemsProcessed(items_processed);
110 state.SetBytesProcessed(items_processed *
sizeof(
v));
113 ->Range(1 << 0, 1 << 10);
116 #ifdef BENCHMARK_HAS_CXX11
121 size_t len =
static_cast<size_t>(
state.range(0));
129 if (
state.thread_index() == 0) {
135 std::lock_guard<std::mutex>
l(test_vector_mu);
142 if (
state.thread_index() == 0) {
151 for (
int i = 0;
i <
state.range(0); ++
i)
159 int thread_size =
static_cast<int>(
size) /
state.threads();
160 int from = thread_size *
state.thread_index();
161 int to =
from + thread_size;
163 if (
state.thread_index() == 0) {
175 if (
state.thread_index() == 0) {
184 std::chrono::duration<double, std::micro> sleep_duration{
185 static_cast<double>(microseconds)};
190 std::this_thread::sleep_for(
191 std::chrono::duration_cast<std::chrono::nanoseconds>(sleep_duration));
195 std::chrono::duration_cast<std::chrono::duration<double>>(
end -
start);
197 state.SetIterationTime(elapsed.count());
198 slept_for += microseconds;
200 state.SetItemsProcessed(slept_for);
205 #ifdef BENCHMARK_HAS_CXX11
207 template <
class...
Args>
212 BENCHMARK_CAPTURE(BM_with_args, int_test, 42, 43, 44);
213 BENCHMARK_CAPTURE(BM_with_args, string_and_pair_test,
std::string(
"abc"),
214 std::pair<int, double>(42, 3.8));
217 while(
state.KeepRunning()) {}
219 BENCHMARK_CAPTURE(BM_non_template_args, basic_test, 0, 0);
221 #endif // BENCHMARK_HAS_CXX11
224 switch (st.
range(0)) {
236 assert(
false &&
"Invalid test case number");