-
Notifications
You must be signed in to change notification settings - Fork 549
/
Copy path7.cpp
86 lines (76 loc) · 1.62 KB
/
7.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <vector>
#include <algorithm>
#include <map>
#include "ticktock.h"
#include "randint.h"
static int ifelse_magic(int x) {
if (x == 0) {
return 233;
} else if (x == 1) {
return 42;
} else if (x == 2) {
return 666;
} else if (x == 3) {
return 985;
} else {
return 211;
}
}
static int switch_magic(int x) {
switch (x) {
case 0:
return 233;
case 1:
return 42;
case 2:
return 666;
case 3:
return 985;
default:
return 211;
}
}
static int map_magic(int x) {
static const std::map<int, int> lut = {
{0, 233},
{1, 42},
{2, 666},
{3, 985},
{4, 211},
};
return lut.at(x);
}
static int array_magic(int x) {
static const int lut[] = {
233,
42,
666,
985,
211,
};
return lut[x];
}
__attribute__((noinline)) void test(int *a, int n, int (*magic)(int)) {
for (int i = 0; i < n; i++) {
a[i] = magic(a[i]);
}
}
int main() {
std::vector<int> a((int)1e7);
std::generate(a.begin(), a.end(), randint<int, 0, 4>);
TICK(ifelse);
test(a.data(), a.size(), ifelse_magic);
TOCK(ifelse);
std::generate(a.begin(), a.end(), randint<int, 0, 4>);
TICK(switch);
test(a.data(), a.size(), switch_magic);
TOCK(switch);
std::generate(a.begin(), a.end(), randint<int, 0, 4>);
TICK(map);
test(a.data(), a.size(), map_magic);
TOCK(map);
std::generate(a.begin(), a.end(), randint<int, 0, 4>);
TICK(array);
test(a.data(), a.size(), array_magic);
TOCK(array);
}