/
bench.c
135 lines (120 loc) · 3.16 KB
/
bench.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <time.h>
#include "cep1000test.h"
#define J 100000
#define K 20000
static double walltime() {
struct timespec tv;
clock_gettime(CLOCK_REALTIME, &tv);
return tv.tv_sec + 1e-9 * tv.tv_nsec;
}
static void snftime(char *buf, size_t n, double time) {
char *units;
if (time < 1e-06) {
units = "ns";
time *= 1e9;
} else if (time < 1e-03) {
units = "us";
time *= 1e6;
} else if (time < 10) {
units = "ms";
time *= 1e3;
} else {
units = "s";
}
snprintf(buf, n, "%.2f %s", time, units);
buf[n - 1] = '\0';
}
double arrmin(double *arr, size_t n) {
double result = arr[0];
for (size_t i = 0; i != n; ++i) {
result = (arr[i] < result) ? arr[i] : result;
}
return result;
}
int main(int argc, char *argv[]) {
char tbuf[100];
initialize_mycallable();
initialize_mycaller();
void *obj_intern = get_f_callable_intern();
void *obj_key = get_f_callable_key();
printf("Direct result: %f\n", func(2.0));
printf("Intern result: %f\n", docall_intern(obj_intern, 2.0));
printf("Key result: %f\n", docall_key(obj_key, 2.0));
printf("Intern+getfunc result: %f\n", docall_getfunc_intern(obj_intern, 2.0));
printf("Key+getfunc result: %f\n", docall_getfunc_key(obj_key, 2.0));
double s = 0;
{
double times[K];
for (int k = 0; k != K; ++k) {
double t0 = walltime();
for (int i = 0; i != J; i++) {
s += func(2.0);
}
times[k] = walltime() - t0;
}
snftime(tbuf, 100, arrmin(times, K) / (double)J);
printf("Direct took %s\n", tbuf);
}
{
double times[K];
for (int k = 0; k != K; ++k) {
double t0 = walltime();
for (int i = 0; i != J; i++) {
s += docall_dispatch(&func, 2.0);
}
times[k] = walltime() - t0;
}
snftime(tbuf, 100, arrmin(times, K) / (double)J);
printf("Dispatch took %s\n", tbuf);
}
{
double times[K];
for (int k = 0; k != K; ++k) {
double t0 = walltime();
for (int i = 0; i != J; i++) {
s += docall_intern(obj_intern, 2.0);
}
times[k] = walltime() - t0;
}
snftime(tbuf, 100, arrmin(times, K) / (double)J);
printf("Intern method took %s\n", tbuf);
}
{
double times[K];
for (int k = 0; k != K; ++k) {
double t0 = walltime();
for (int i = 0; i != J; i++) {
s += docall_key(obj_key, 2.0);
}
times[k] = walltime() - t0;
}
snftime(tbuf, 100, arrmin(times, K) / (double)J);
printf("Key method took %s\n", tbuf);
}
{
double times[K];
for (int k = 0; k != K; ++k) {
double t0 = walltime();
for (int i = 0; i != J; i++) {
s += docall_getfunc_intern(obj_intern, 2.0);
}
times[k] = walltime() - t0;
}
snftime(tbuf, 100, arrmin(times, K) / (double)J);
printf("Intern+getfunc method took %s\n", tbuf);
}
{
double times[K];
for (int k = 0; k != K; ++k) {
double t0 = walltime();
for (int i = 0; i != J; i++) {
s += docall_getfunc_key(obj_key, 2.0);
}
times[k] = walltime() - t0;
}
snftime(tbuf, 100, arrmin(times, K) / (double)J);
printf("Key+getfunc method took %s\n", tbuf);
}
printf("s: %f\n", s);
return 0;
}