-
Notifications
You must be signed in to change notification settings - Fork 0
/
06a_func_branch.cpp
41 lines (35 loc) · 1002 Bytes
/
06a_func_branch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <memory>
#include "benchmark/benchmark.h"
int __attribute__ ((noinline)) f1(int i) { return -i; }
int __attribute__ ((noinline)) f2(int i) { return i + 1; }
void BM_branch(benchmark::State& state) {
srand(1);
const unsigned int N = state.range(0);
std::unique_ptr<bool[]> b1(new bool[N]);
std::vector<int> v1(N);
for (size_t i = 0; i < N; ++i) {
v1[i] = rand();
b1[i] = rand() & 0x1;
}
const bool* const b = &b1[0];
const int* const v = &v1[0];
for (auto _ : state) {
unsigned long a1 = 0;
for (size_t i = 0; i < N; ++i) {
if (b[i]) {
a1 += f1(v[i]);
} else {
a1 += f2(v[i]);
}
}
benchmark::DoNotOptimize(a1);
benchmark::ClobberMemory();
}
state.SetItemsProcessed(N*state.iterations());
}
BENCHMARK(BM_branch)->Arg(1<<22);
BENCHMARK_MAIN();