Correct code and enable tests for ETL "spline" and "value"
[synfig.git] / ETL / test / benchmark.cpp
1 /*! ========================================================================
2 ** Extended Template and Library Test Suite
3 ** Hermite Curve Test
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 **
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
12 **
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ** General Public License for more details.
17 **
18 ** === N O T E S ===========================================================
19 **
20 ** ========================================================================= */
21
22 /* === H E A D E R S ======================================================= */
23
24 #include <ETL/clock>
25 #include <ETL/hermite>
26 #include <ETL/angle>
27 #include <ETL/fastangle>
28 #include <ETL/fixed>
29 #include <ETL/surface>
30 #include <ETL/gaussian>
31 #include <ETL/calculus>
32 #include <stdio.h>
33
34 /* === M A C R O S ========================================================= */
35
36 using namespace etl;
37 //using namespace std;
38
39 #define HERMITE_TEST_ITERATIONS         (100000)
40
41 /* === C L A S S E S ======================================================= */
42
43 /* === P R O C E D U R E S ================================================= */
44
45 template <class Angle>
46 void angle_cos_speed_test(void)
47 {
48         Angle a,b,c,d;
49         float tmp,tmp2;
50
51         for(tmp=-1;tmp<1;tmp+=0.000002f)
52         {
53                 a=(typename Angle::cos)(tmp);
54                 b=(typename Angle::cos)(tmp);
55                 c=(typename Angle::cos)(tmp);
56                 d=(typename Angle::cos)(tmp);
57                 tmp2=((typename Angle::cos)(a)).get();
58                 tmp2=((typename Angle::cos)(b)).get();
59                 tmp2=((typename Angle::cos)(c)).get();
60                 tmp2=((typename Angle::cos)(d)).get();
61         }
62 }
63 template <class Angle>
64 void angle_sin_speed_test(void)
65 {
66         Angle a,b,c,d;
67         float tmp,tmp2;
68
69         for(tmp=-1.0;tmp<1.0;tmp+=0.000002f)
70         {
71                 a=(typename Angle::sin)(tmp);
72                 b=(typename Angle::sin)(tmp);
73                 c=(typename Angle::sin)(tmp);
74                 d=(typename Angle::sin)(tmp);
75                 tmp2=((typename Angle::sin)(a)).get();
76                 tmp2=((typename Angle::sin)(b)).get();
77                 tmp2=((typename Angle::sin)(c)).get();
78                 tmp2=((typename Angle::sin)(d)).get();
79         }
80 }
81 template <class Angle>
82 void angle_tan_speed_test(void)
83 {
84         Angle a,b,c,d;
85         float tmp,tmp2;
86
87         for(tmp=-1;tmp<1;tmp+=0.000002f)
88         {
89                 a=(typename Angle::tan)(tmp);
90                 b=(typename Angle::tan)(tmp);
91                 c=(typename Angle::tan)(tmp);
92                 d=(typename Angle::tan)(tmp);
93                 tmp2=((typename Angle::tan)(a)).get();
94                 tmp2=((typename Angle::tan)(b)).get();
95                 tmp2=((typename Angle::tan)(c)).get();
96                 tmp2=((typename Angle::tan)(d)).get();
97         }
98 }
99 template <class Angle, class mytan>
100 void angle_atan2_speed_test(void)
101 {
102         Angle a,b,c;
103         float x,y;
104
105         for(y=-10;y<10;y+=0.05f)
106                 for(x=-10;x<10;x+=0.05f)
107                 {
108                         a=mytan(y,x);
109                         a=mytan(x,y);
110                         b=mytan(y,x);
111                         b=mytan(x,y);
112                         c=mytan(y,x);
113                         c=mytan(x,y);
114                         a=mytan(y,x);
115                         a=mytan(x,y);
116                         b=mytan(y,x);
117                         b=mytan(x,y);
118                         c=mytan(y,x);
119                         c=mytan(x,y);
120                 }
121 }
122
123 int fastangle_speed_test(void)
124 {
125         int ret=0;
126         float
127                 angle_cos_time,
128                 fastangle_cos_time,
129                 angle_tan_time,
130                 fastangle_tan_time,
131                 angle_atan2_time,
132                 fastangle_atan2_time,
133                 angle_sin_time,
134                 fastangle_sin_time ;
135
136         etl::clock MyTimer;
137
138         MyTimer.reset();
139         angle_cos_speed_test<angle>();
140         angle_cos_time=MyTimer();
141         printf("angle: Cosine test: %f seconds\n",angle_cos_time);
142
143         MyTimer.reset();
144         angle_cos_speed_test<fastangle>();
145         fastangle_cos_time=MyTimer();
146         printf("fastangle: Cosine test: %f seconds\n",fastangle_cos_time);
147         printf("fastangle is %.02f%% faster\n",(angle_cos_time/fastangle_cos_time)*100.0-100.0);
148
149         MyTimer.reset();
150         angle_sin_speed_test<angle>();
151         angle_sin_time=MyTimer();
152         printf("angle: Sine test: %f seconds\n",angle_sin_time);
153
154         MyTimer.reset();
155         angle_sin_speed_test<fastangle>();
156         fastangle_sin_time=MyTimer();
157         printf("fastangle: Sine test: %f seconds\n",fastangle_sin_time);
158         printf("fastangle is %.02f%% faster\n",(angle_sin_time/fastangle_sin_time)*100.0-100.0);
159
160         MyTimer.reset();
161         angle_tan_speed_test<angle>();
162         angle_tan_time=MyTimer();
163         printf("angle: Tangent test: %f seconds\n",angle_tan_time);
164
165         MyTimer.reset();
166         angle_tan_speed_test<fastangle>();
167         fastangle_tan_time=MyTimer();
168         printf("fastangle: Tangent test: %f seconds\n",fastangle_tan_time);
169         printf("fastangle is %.02f%% faster\n",(angle_tan_time/fastangle_tan_time)*100.0-100.0);
170
171         MyTimer.reset();
172         angle_atan2_speed_test<angle,angle::tan>();
173         angle_atan2_time=MyTimer();
174         printf("angle: arcTangent2 test: %f seconds\n",angle_atan2_time);
175
176         MyTimer.reset();
177         angle_atan2_speed_test<fastangle,fastangle::tan>();
178         fastangle_atan2_time=MyTimer();
179         printf("fastangle: arcTangent2 test: %f seconds\n",fastangle_atan2_time);
180         printf("fastangle is %.02f%% faster\n",(angle_atan2_time/fastangle_atan2_time)*100.0-100.0);
181
182         return ret;
183 }
184
185 int surface_and_gaussian_blur_test()
186 {
187         int ret=0;
188         etl::clock MyTimer;
189         float endtime;
190
191         {
192                 surface<float> my_surface(1000,1000);
193
194                 MyTimer.reset();
195                 gaussian_blur(my_surface.begin(),my_surface.end(),30,30);
196                 endtime=MyTimer();
197                 printf("surface_and_gaussian_blur_test<float>: %f seconds\n",endtime);
198         }
199
200         {
201                 surface<double> my_surface(1000,1000);
202
203                 MyTimer.reset();
204                 gaussian_blur(my_surface.begin(),my_surface.end(),30,30);
205                 endtime=MyTimer();
206                 printf("surface_and_gaussian_blur_test<double>: %f seconds\n",endtime);
207         }
208
209         {
210                 surface<fixed> my_surface(1000,1000);
211
212                 MyTimer.reset();
213                 gaussian_blur(my_surface.begin(),my_surface.end(),30,30);
214                 endtime=MyTimer();
215                 printf("surface_and_gaussian_blur_test<fixed>: %f seconds\n",endtime);
216         }
217
218         return ret;
219 }
220
221 int hermite_int_test()
222 {
223         int ret=0;
224         hermite<int> Hermie;
225         hermite<int>::time_type f;
226         int i;
227
228         etl::clock timer;
229         etl::clock::value_type t;
230
231         Hermie.p1()=0;
232         Hermie.t1()=40000;
233         Hermie.p2()=0;
234         Hermie.t2()=40000;
235
236         Hermie.sync();
237
238         {float t;
239         for(f=0.0f,i=0,timer.reset();i<HERMITE_TEST_ITERATIONS;i++,f+=0.000005f)
240         {
241                 t+=Hermie(f)+Hermie(f+0.1f);
242                 t+=Hermie(f)+Hermie(f+0.1f);
243                 t+=Hermie(f)+Hermie(f+0.1f);
244                 t+=Hermie(f)+Hermie(f+0.1f);
245                 t+=Hermie(f)+Hermie(f+0.1f);
246                 t+=Hermie(f)+Hermie(f+0.1f);
247                 t+=Hermie(f)+Hermie(f+0.1f);
248                 t+=Hermie(f)+Hermie(f+0.1f);
249                 t+=Hermie(f)+Hermie(f+0.1f);
250                 t+=Hermie(f)+Hermie(f+0.1f);
251                 t+=Hermie(f)+Hermie(f+0.1f);
252                 t+=Hermie(f)+Hermie(f+0.1f);
253         }
254         }
255         t=timer();
256
257         printf("hermite<int>:time=%f milliseconds\n",t*1000);
258         return ret;
259 }
260
261 int hermite_float_test(void)
262 {
263         int ret=0;
264         float f; int i;
265
266         hermite<float> Hermie;
267         etl::clock timer;
268         double t;
269
270         Hermie.p1()=0;
271         Hermie.t1()=1;
272         Hermie.p2()=0;
273         Hermie.t2()=1;
274
275         Hermie.sync();
276
277         {float t;
278         for(f=0.0f,i=0,timer.reset();i<HERMITE_TEST_ITERATIONS;i++,f+=0.000005f)
279         {
280                 t+=Hermie(f)+Hermie(f+0.1f);
281                 t+=Hermie(f)+Hermie(f+0.1f);
282                 t+=Hermie(f)+Hermie(f+0.1f);
283                 t+=Hermie(f)+Hermie(f+0.1f);
284                 t+=Hermie(f)+Hermie(f+0.1f);
285                 t+=Hermie(f)+Hermie(f+0.1f);
286                 t+=Hermie(f)+Hermie(f+0.1f);
287                 t+=Hermie(f)+Hermie(f+0.1f);
288                 t+=Hermie(f)+Hermie(f+0.1f);
289                 t+=Hermie(f)+Hermie(f+0.1f);
290                 t+=Hermie(f)+Hermie(f+0.1f);
291                 t+=Hermie(f)+Hermie(f+0.1f);
292         }
293         }
294         t=timer();
295
296         printf("hermite<float>:time=%f milliseconds\n",t*1000);
297         return ret;
298 }
299
300 int hermite_double_test(void)
301 {
302         int ret=0,i;
303         float f;
304
305         hermite<double> Hermie;
306         etl::clock timer;
307         double t;
308
309         Hermie.p1()=0;
310         Hermie.t1()=1;
311         Hermie.p2()=0;
312         Hermie.t2()=1;
313
314         Hermie.sync();
315
316         for(f=0.0f,i=0,timer.reset();i<HERMITE_TEST_ITERATIONS;i++,f+=0.000005f)
317         {
318                 t+=Hermie(f)+Hermie(f+0.1f);
319                 t+=Hermie(f)+Hermie(f+0.1f);
320                 t+=Hermie(f)+Hermie(f+0.1f);
321                 t+=Hermie(f)+Hermie(f+0.1f);
322                 t+=Hermie(f)+Hermie(f+0.1f);
323                 t+=Hermie(f)+Hermie(f+0.1f);
324                 t+=Hermie(f)+Hermie(f+0.1f);
325                 t+=Hermie(f)+Hermie(f+0.1f);
326                 t+=Hermie(f)+Hermie(f+0.1f);
327                 t+=Hermie(f)+Hermie(f+0.1f);
328                 t+=Hermie(f)+Hermie(f+0.1f);
329                 t+=Hermie(f)+Hermie(f+0.1f);
330         }
331         t=timer();
332
333         printf("hermite<double>:time=%f milliseconds\n",t*1000);
334         return ret;
335 }
336
337 int hermite_fixed_test(void)
338 {
339         int ret=0;
340     int i;
341         hermite<fixed> Hermie;
342         hermite<fixed>::time_type f;
343         hermite<fixed>::time_type inc(0.0005f), inc2(1.10);
344         fixed sum(0);
345
346         etl::clock timer;
347         double t;
348
349         Hermie.p1()=0;
350         Hermie.t1()=1;
351         Hermie.p2()=0;
352         Hermie.t2()=1;
353
354         Hermie.sync();
355
356         {fixed t;
357         for(i=0,f=0,timer.reset();i<HERMITE_TEST_ITERATIONS;i++,f+=inc)
358         {
359                 sum+=Hermie(f)+Hermie(f+inc2);
360                 sum+=Hermie(f)+Hermie(f+inc2);
361                 sum+=Hermie(f)+Hermie(f+inc2);
362                 sum+=Hermie(f)+Hermie(f+inc2);
363                 sum+=Hermie(f)+Hermie(f+inc2);
364                 sum+=Hermie(f)+Hermie(f+inc2);
365                 sum+=Hermie(f)+Hermie(f+inc2);
366                 sum+=Hermie(f)+Hermie(f+inc2);
367                 sum+=Hermie(f)+Hermie(f+inc2);
368                 sum+=Hermie(f)+Hermie(f+inc2);
369                 sum+=Hermie(f)+Hermie(f+inc2);
370                 sum+=Hermie(f)+Hermie(f+inc2);
371         }
372         }
373         t=timer();
374
375         printf("hermite<fixed>:time=%f milliseconds\n",t*1000);
376         return ret;
377 }
378
379 int hermite_angle_test(void)
380 {
381         int ret=0,i;
382         float f;
383
384         hermite<angle> Hermie;
385         etl::clock timer;
386         angle tmp;
387         double t;
388
389         Hermie.p1()=angle::degrees(0);
390         Hermie.t1()=angle::degrees(45);
391
392         Hermie.p2()=angle::degrees(-45);
393         Hermie.t2()=angle::degrees(180);
394
395         Hermie.sync();
396
397         for(f=0.0f,i=0,timer.reset();i<HERMITE_TEST_ITERATIONS;i++,f+=0.000005f)
398         {
399                 tmp+=Hermie(f)+Hermie(f+0.1f);
400                 tmp+=Hermie(f)+Hermie(f+0.1f);
401                 tmp+=Hermie(f)+Hermie(f+0.1f);
402                 tmp+=Hermie(f)+Hermie(f+0.1f);
403                 tmp+=Hermie(f)+Hermie(f+0.1f);
404                 tmp+=Hermie(f)+Hermie(f+0.1f);
405                 tmp+=Hermie(f)+Hermie(f+0.1f);
406                 tmp+=Hermie(f)+Hermie(f+0.1f);
407                 tmp+=Hermie(f)+Hermie(f+0.1f);
408                 tmp+=Hermie(f)+Hermie(f+0.1f);
409                 tmp+=Hermie(f)+Hermie(f+0.1f);
410                 tmp+=Hermie(f)+Hermie(f+0.1f);
411         }
412         t=timer();
413
414         fprintf(stderr,"hermite<angle>:time=%f milliseconds\n",t*1000);
415
416         return ret;
417 }
418
419 int hermite_fastangle_test(void)
420 {
421         int ret=0,i;
422         hermite<fastangle> Hermie;
423         hermite<fastangle>::time_type f;
424
425         etl::clock timer;
426         fastangle tmp;
427         double t;
428
429         Hermie.p1()=fastangle::degrees(0);
430         Hermie.t1()=fastangle::degrees(45);
431
432         Hermie.p2()=fastangle::degrees(-45);
433         Hermie.t2()=fastangle::degrees(180);
434
435         Hermie.sync();
436
437         for(f=0.0f,i=0,timer.reset();i<HERMITE_TEST_ITERATIONS;i++,f+=0.000005f)
438         {
439                 tmp+=Hermie(f)+Hermie(f+0.1f);
440                 tmp+=Hermie(f)+Hermie(f+0.1f);
441                 tmp+=Hermie(f)+Hermie(f+0.1f);
442                 tmp+=Hermie(f)+Hermie(f+0.1f);
443                 tmp+=Hermie(f)+Hermie(f+0.1f);
444                 tmp+=Hermie(f)+Hermie(f+0.1f);
445                 tmp+=Hermie(f)+Hermie(f+0.1f);
446                 tmp+=Hermie(f)+Hermie(f+0.1f);
447                 tmp+=Hermie(f)+Hermie(f+0.1f);
448                 tmp+=Hermie(f)+Hermie(f+0.1f);
449                 tmp+=Hermie(f)+Hermie(f+0.1f);
450                 tmp+=Hermie(f)+Hermie(f+0.1f);
451         }
452         t=timer();
453
454         fprintf(stderr,"hermite<fastangle>:time=%f milliseconds\n",t*1000);
455
456         return ret;
457 }
458
459 /* === E N T R Y P O I N T ================================================= */
460
461 int main()
462 {
463         int error=0;
464
465         error+=fastangle_speed_test();
466         error+=surface_and_gaussian_blur_test();
467         error+=hermite_float_test();
468         error+=hermite_double_test();
469         error+=hermite_int_test();
470         error+=hermite_fixed_test();
471         error+=hermite_angle_test();
472         error+=hermite_fastangle_test();
473
474         return error;
475 }