-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_LUT.cpp
159 lines (118 loc) · 2.84 KB
/
test_LUT.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <vector>
#include <random>
#include <stdio.h>
#include <cmath>
#include <sys/time.h>
#include <unistd.h>
#include "table_lut.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
*/
float process_virtual(TV* tv, const uint8_t *codes) {
float themax = 0;
for (int i = 0; i < N; i++) {
float v = tv->f(codes);
if (v > themax) {
themax = v;
}
}
return themax;
}
struct TVt {
std::vector<float> lut;
TVt() {
lut.resize(M2 * 256);
std::mt19937 rng(123);
for(int i = 0; i < lut.size(); i++) {
lut[i] = rng() / float(rng.max());
// printf("lut[%d]=%g\n", i, lut[i]);
}
};
float f(const uint8_t *codes) {
const float *luti = lut.data();
float accu = 0;
for(int i = 0; i < M2; i++) {
accu += luti[codes[i]];
luti += 256;
}
return accu;
}
};
template<class TV>
float process_template(TV* tv, const uint8_t *codes) {
float themax = 0;
for (int i = 0; i < N; i++) {
float v = tv->f(codes);
if (v > themax) {
themax = v;
}
}
return themax;
}
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);
std::vector<uint8_t> codes(M2 * N);
{
std::mt19937 rng(1234);
for(int i = 0; i < codes.size(); i++) {
codes[i] = rng() % 0xff;
}
}
for(int run = 0; run < nrun; run++) {
printf("Run %d\n", run);
{
TVI tvi;
double t0 = getmillisecs();
float cs = process_virtual(&tvi, codes.data());
double t1 = getmillisecs();
if (run != 0) {
printf("virtual cs=%g, time= %.3f ms\n", cs, t1 - t0);
}
times[run] = t1 - t0;
}
{
TVt tvt;
double t0 = getmillisecs();
float cs = process_template(&tvt, codes.data());
double t1 = getmillisecs();
if (run != 0) {
printf("template cs=%g, 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;
}