-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_template.cpp
134 lines (98 loc) · 2.34 KB
/
test_template.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
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
#include <vector>
#include <stdio.h>
#include <cmath>
#include <sys/time.h>
#include <unistd.h>
#include "tv.h"
double getmillisecs() {
struct timeval tv;
gettimeofday(&tv, nullptr);
return tv.tv_sec * 1e3 + tv.tv_usec * 1e-3;
}
/*
#define N 10000000
#define M 100
#define M2 10
*/
int process_virtual(TV* tv) {
int accu = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
accu += i ^ j;
}
accu = tv->f(accu);
}
return accu;
}
struct TVt {
int f(int accu2) {
for (int i = 0; i < M2; i++) {
accu2 += accu2 | i;
}
return accu2;
}
};
template<class TV>
int process_template(TV * tv) {
int accu = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
accu += i ^ j;
}
accu = tv->f(accu);
}
return accu;
}
double mean(const double *x, int n) {
double sum = 0;
for(int i = 0; i < n; i++) {
sum += x[i];
}
return sum / n;
}
double stddev(const double *x, int n) {
double sum = 0, sum2 = 0;
for(int i = 0; i < n; i++) {
sum += x[i];
sum2 += x[i] * x[i];
}
return sqrt(sum2 / n - sum * sum / (n * n));
}
int main () {
int nrun = 6;
std::vector<double> times(2 * nrun);
for(int run = 0; run < nrun; run++) {
printf("Run %d\n", run);
{
TVI tvi;
double t0 = getmillisecs();
int cs = process_virtual(&tvi);
double t1 = getmillisecs();
if (run != 0) {
printf("virtual cs=%d, time= %.3f ms\n", cs, t1 - t0);
}
times[run] = t1 - t0;
}
{
TVt tvt;
double t0 = getmillisecs();
int cs = process_template(&tvt);
double t1 = getmillisecs();
if (run != 0) {
printf("template cs=%d, time= %.3f ms\n", cs, t1 - t0);
}
times[run + nrun] = t1 - t0;
}
}
double mean_virtual = mean(times.data() + 1, nrun - 1);
double mean_template = mean(times.data() + 1 + nrun, nrun - 1);
printf("virtual time: %.3f +/- %.3f\n",
mean_virtual,
stddev(times.data() + 1, nrun - 1));
printf("template time: %.3f +/- %.3f (%+.2f %%)\n",
mean_template,
stddev(times.data() + 1 + nrun, nrun - 1),
(mean_template - mean_virtual) * 100.0 / mean_virtual
);
return 0;
}