Mercurial > hg > silvet
comparison bqvec/test/Timings.cpp @ 372:af71cbdab621 tip
Update bqvec code
author | Chris Cannam |
---|---|
date | Tue, 19 Nov 2019 10:13:32 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
371:426ce52ef096 | 372:af71cbdab621 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 #include "bqvec/VectorOpsComplex.h" | |
4 | |
5 #include <iostream> | |
6 #include <cstdlib> | |
7 | |
8 #include <time.h> | |
9 | |
10 using namespace std; | |
11 using namespace breakfastquay; | |
12 | |
13 //!!! This is nonsense. TODO: Replace it with sense. | |
14 | |
15 #ifdef _WIN32 | |
16 #define drand48() (-1+2*((float)rand())/RAND_MAX) | |
17 #endif | |
18 | |
19 bool | |
20 testMultiply() | |
21 { | |
22 cerr << "testVectorOps: testing v_multiply complex" << endl; | |
23 | |
24 const int N = 1024; | |
25 //!!! todo: use aligned allocate(), otherwise results will vary randomly | |
26 bq_complex_t target[N]; | |
27 bq_complex_t src1[N]; | |
28 bq_complex_t src2[N]; | |
29 | |
30 for (int i = 0; i < N; ++i) { | |
31 src1[i].re = drand48(); | |
32 src1[i].im = drand48(); | |
33 src2[i].re = drand48(); | |
34 src2[i].im = drand48(); | |
35 } | |
36 | |
37 double mean, first, last, total = 0; | |
38 for (int i = 0; i < N; ++i) { | |
39 bq_complex_t result; | |
40 c_multiply(result, src1[i], src2[i]); | |
41 if (i == 0) first = result.re; | |
42 if (i == N-1) last = result.im; | |
43 total += result.re; | |
44 total += result.im; | |
45 } | |
46 mean = total / (N*2); | |
47 cerr << "Naive method: mean = " << mean << ", first = " << first | |
48 << ", last = " << last << endl; | |
49 | |
50 v_multiply_to(target, src1, src2, N); | |
51 total = 0; | |
52 | |
53 for (int i = 0; i < N; ++i) { | |
54 if (i == 0) first = target[i].re; | |
55 if (i == N-1) last = target[i].im; | |
56 total += target[i].re; | |
57 total += target[i].im; | |
58 } | |
59 mean = total / (N*2); | |
60 cerr << "v_multiply: mean = " << mean << ", first = " << first | |
61 << ", last = " << last << endl; | |
62 | |
63 int iterations = 50000; | |
64 // cerr << "Iterations: " << iterations << endl; | |
65 | |
66 // cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; | |
67 float divisor = float(CLOCKS_PER_SEC) / 1000.f; | |
68 | |
69 clock_t start = clock(); | |
70 | |
71 for (int j = 0; j < iterations; ++j) { | |
72 for (int i = 0; i < N; ++i) { | |
73 c_multiply(target[i], src1[i], src2[i]); | |
74 } | |
75 } | |
76 | |
77 clock_t end = clock(); | |
78 | |
79 cerr << "Time for naive method: " << float(end - start)/divisor << endl; | |
80 | |
81 start = clock(); | |
82 | |
83 for (int j = 0; j < iterations; ++j) { | |
84 v_multiply_to(target, src1, src2, N); | |
85 } | |
86 | |
87 end = clock(); | |
88 | |
89 cerr << "Time for v_multiply: " << float(end - start)/divisor << endl; | |
90 | |
91 return true; | |
92 } | |
93 | |
94 bool | |
95 testPolarToCart() | |
96 { | |
97 cerr << "testVectorOps: testing v_polar_to_cartesian" << endl; | |
98 | |
99 const int N = 1024; | |
100 bq_complex_t target[N]; | |
101 bq_complex_element_t mag[N]; | |
102 bq_complex_element_t phase[N]; | |
103 | |
104 for (int i = 0; i < N; ++i) { | |
105 mag[i] = drand48(); | |
106 phase[i] = (drand48() * M_PI * 2) - M_PI; | |
107 } | |
108 | |
109 double mean, first, last, total = 0; | |
110 for (int i = 0; i < N; ++i) { | |
111 double real = mag[i] * cos(phase[i]); | |
112 double imag = mag[i] * sin(phase[i]); | |
113 if (i == 0) first = real; | |
114 if (i == N-1) last = imag; | |
115 total += real; | |
116 total += imag; | |
117 } | |
118 mean = total / (N*2); | |
119 cerr << "Naive method: mean = " << mean << ", first = " << first | |
120 << ", last = " << last << endl; | |
121 | |
122 v_polar_to_cartesian(target, mag, phase, N); | |
123 | |
124 total = 0; | |
125 | |
126 for (int i = 0; i < N; ++i) { | |
127 if (i == 0) first = target[i].re; | |
128 if (i == N-1) last = target[i].im; | |
129 total += target[i].re; | |
130 total += target[i].im; | |
131 } | |
132 mean = total / (N*2); | |
133 cerr << "v_polar_to_cartesian: mean = " << mean << ", first = " << first | |
134 << ", last = " << last << endl; | |
135 | |
136 int iterations = 10000; | |
137 // cerr << "Iterations: " << iterations << endl; | |
138 | |
139 // cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; | |
140 float divisor = float(CLOCKS_PER_SEC) / 1000.f; | |
141 | |
142 clock_t start = clock(); | |
143 | |
144 for (int j = 0; j < iterations; ++j) { | |
145 for (int i = 0; i < N; ++i) { | |
146 target[i].re = mag[i] * cos(phase[i]); | |
147 target[i].im = mag[i] * sin(phase[i]); | |
148 } | |
149 } | |
150 | |
151 clock_t end = clock(); | |
152 | |
153 cerr << "Time for naive method: " << float(end - start)/divisor << endl; | |
154 | |
155 start = clock(); | |
156 | |
157 for (int j = 0; j < iterations; ++j) { | |
158 v_polar_to_cartesian(target, mag, phase, N); | |
159 } | |
160 | |
161 end = clock(); | |
162 | |
163 cerr << "Time for v_polar_to_cartesian: " << float(end - start)/divisor << endl; | |
164 | |
165 return true; | |
166 } | |
167 | |
168 bool | |
169 testPolarToCartInterleaved() | |
170 { | |
171 cerr << "testVectorOps: testing v_polar_interleaved_to_cartesian" << endl; | |
172 | |
173 const int N = 1024; | |
174 bq_complex_t target[N]; | |
175 bq_complex_element_t source[N*2]; | |
176 | |
177 for (int i = 0; i < N; ++i) { | |
178 source[i*2] = drand48(); | |
179 source[i*2+1] = (drand48() * M_PI * 2) - M_PI; | |
180 } | |
181 | |
182 double mean, first, last, total = 0; | |
183 for (int i = 0; i < N; ++i) { | |
184 double real = source[i*2] * cos(source[i*2+1]); | |
185 double imag = source[i*2] * sin(source[i*2+1]); | |
186 if (i == 0) first = real; | |
187 if (i == N-1) last = imag; | |
188 total += real; | |
189 total += imag; | |
190 } | |
191 mean = total / (N*2); | |
192 cerr << "Naive method: mean = " << mean << ", first = " << first | |
193 << ", last = " << last << endl; | |
194 | |
195 v_polar_interleaved_to_cartesian(target, source, N); | |
196 | |
197 total = 0; | |
198 | |
199 for (int i = 0; i < N; ++i) { | |
200 if (i == 0) first = target[i].re; | |
201 if (i == N-1) last = target[i].im; | |
202 total += target[i].re; | |
203 total += target[i].im; | |
204 } | |
205 mean = total / (N*2); | |
206 cerr << "v_polar_interleaved_to_cartesian: mean = " << mean << ", first = " << first | |
207 << ", last = " << last << endl; | |
208 | |
209 int iterations = 10000; | |
210 // cerr << "Iterations: " << iterations << endl; | |
211 | |
212 // cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; | |
213 float divisor = float(CLOCKS_PER_SEC) / 1000.f; | |
214 | |
215 clock_t start = clock(); | |
216 | |
217 for (int j = 0; j < iterations; ++j) { | |
218 for (int i = 0; i < N; ++i) { | |
219 target[i].re = source[i*2] * cos(source[i*2+1]); | |
220 target[i].im = source[i*2] * sin(source[i*2+1]); | |
221 } | |
222 } | |
223 | |
224 clock_t end = clock(); | |
225 | |
226 cerr << "Time for naive method: " << float(end - start)/divisor << endl; | |
227 | |
228 start = clock(); | |
229 | |
230 for (int j = 0; j < iterations; ++j) { | |
231 v_polar_interleaved_to_cartesian(target, source, N); | |
232 } | |
233 | |
234 end = clock(); | |
235 | |
236 cerr << "Time for v_polar_interleaved_to_cartesian: " << float(end - start)/divisor << endl; | |
237 | |
238 return true; | |
239 } | |
240 | |
241 bool | |
242 testCartToPolar() | |
243 { | |
244 cerr << "testVectorOps: testing v_cartesian_to_polar" << endl; | |
245 | |
246 const int N = 1024; | |
247 bq_complex_t source[N]; | |
248 bq_complex_element_t mag[N]; | |
249 bq_complex_element_t phase[N]; | |
250 | |
251 for (int i = 0; i < N; ++i) { | |
252 source[i].re = (drand48() * 2.0) - 1.0; | |
253 source[i].im = (drand48() * 2.0) - 1.0; | |
254 } | |
255 | |
256 double mean, first, last, total = 0; | |
257 for (int i = 0; i < N; ++i) { | |
258 double mag = sqrt(source[i].re * source[i].re + source[i].im * source[i].im); | |
259 double phase = atan2(source[i].im, source[i].re); | |
260 if (i == 0) first = mag; | |
261 if (i == N-1) last = phase; | |
262 total += mag; | |
263 total += phase; | |
264 } | |
265 mean = total / (N*2); | |
266 cerr << "Naive method: mean = " << mean << ", first = " << first | |
267 << ", last = " << last << endl; | |
268 | |
269 v_cartesian_to_polar(mag, phase, source, N); | |
270 | |
271 total = 0; | |
272 | |
273 for (int i = 0; i < N; ++i) { | |
274 if (i == 0) first = mag[i]; | |
275 if (i == N-1) last = phase[i]; | |
276 total += mag[i]; | |
277 total += phase[i]; | |
278 } | |
279 mean = total / (N*2); | |
280 cerr << "v_cartesian_to_polar: mean = " << mean << ", first = " << first | |
281 << ", last = " << last << endl; | |
282 | |
283 int iterations = 10000; | |
284 // cerr << "Iterations: " << iterations << endl; | |
285 | |
286 // cerr << "CLOCKS_PER_SEC = " << CLOCKS_PER_SEC << endl; | |
287 float divisor = float(CLOCKS_PER_SEC) / 1000.f; | |
288 | |
289 clock_t start = clock(); | |
290 | |
291 for (int j = 0; j < iterations; ++j) { | |
292 for (int i = 0; i < N; ++i) { | |
293 mag[i] = sqrt(source[i].re * source[i].re + source[i].im * source[i].im); | |
294 phase[i] = atan2(source[i].im, source[i].re); | |
295 } | |
296 } | |
297 | |
298 clock_t end = clock(); | |
299 | |
300 cerr << "Time for naive method: " << float(end - start)/divisor << endl; | |
301 | |
302 start = clock(); | |
303 | |
304 for (int j = 0; j < iterations; ++j) { | |
305 v_cartesian_to_polar(mag, phase, source, N); | |
306 } | |
307 | |
308 end = clock(); | |
309 | |
310 cerr << "Time for v_cartesian_to_polar: " << float(end - start)/divisor << endl; | |
311 | |
312 return true; | |
313 } | |
314 | |
315 int main(int, char **) | |
316 { | |
317 if (!testMultiply()) return 1; | |
318 if (!testPolarToCart()) return 1; | |
319 if (!testPolarToCartInterleaved()) return 1; | |
320 if (!testCartToPolar()) return 1; | |
321 return 0; | |
322 } | |
323 |