-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindirectcall.c
118 lines (97 loc) · 2.2 KB
/
indirectcall.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "benchmark.h"
#define OP_TYPE_NUM 5
#define RAND_ARRAY_LEN 256
#define FUNC_RETURN_TYPE void
#define FUNC_PARAMETER void
#define OPS \
_(open) \
_(close) \
_(write) \
_(read) \
_(add) \
_(delete)
#define _(op) typedef FUNC_RETURN_TYPE (*op)(FUNC_PARAMETER);
OPS
#undef _
enum types {
red,
blue,
yellow,
black,
white
};
#define LIST \
_(red) \
_(blue)\
_(yellow)\
_(black)\
_(white)
#define FUNC_BODY {int i=0; i++;}
#undef FUNC_RETURN_TYPE
#define FUNC_RETURN_TYPE static void
#define _(color) FUNC_RETURN_TYPE open_##color(FUNC_PARAMETER) FUNC_BODY \
FUNC_RETURN_TYPE close_##color(FUNC_PARAMETER) FUNC_BODY \
FUNC_RETURN_TYPE write_##color(FUNC_PARAMETER) FUNC_BODY \
FUNC_RETURN_TYPE read_##color(FUNC_PARAMETER) FUNC_BODY \
FUNC_RETURN_TYPE add_##color(FUNC_PARAMETER) FUNC_BODY \
FUNC_RETURN_TYPE delete_##color(FUNC_PARAMETER) FUNC_BODY
LIST
#undef _
#define _(op) op op##_fp;
struct op {
OPS
};
#undef _
struct op ops[OP_TYPE_NUM];
int rand_array[RAND_ARRAY_LEN];
static
void call_ops(void) {
int i = 0;
for(; i < RAND_ARRAY_LEN; i++) {
int idx = rand_array[i];
ops[idx].open_fp();
}
}
static
void call_ops_directly(void) {
int i = 0;
for(; i < RAND_ARRAY_LEN; i++) {
int idx = rand_array[i];
switch(idx) {
#define _(color) case color: open_##color(); break;
LIST
#undef _
default: open_red(); break;
}
}
}
int
main(void)
{
#define _(color) ops[color].open_fp = &open_##color; \
ops[color].close_fp = &close_##color; \
ops[color].write_fp = &write_##color; \
ops[color].read_fp = &read_##color; \
ops[color].add_fp = &add_##color; \
ops[color].delete_fp = &delete_##color;
LIST
#undef _
int i = 0;
for(; i < RAND_ARRAY_LEN; i++) {
srand((unsigned)time(NULL));
#ifdef RANDOM_IDX
rand_array[i] = rand() % OP_TYPE_NUM;
#else
rand_array[i] = 1;
#endif
}
#ifdef INDIRECT_CALL
BEST_TIME_NOCHECK(call_ops(), ,1000000, 1, 1);
#else
BEST_TIME_NOCHECK(call_ops_directly(), ,1000000, 1, 1);
#endif
return 0;
}