]> Creatis software - gdcm.git/blob - Testing/TestInline.cxx
* Improvement of the TestInline
[gdcm.git] / Testing / TestInline.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestInline.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/29 13:02:44 $
7   Version:   $Revision: 1.10 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18 // This test is expected to 'show' the actual effect on an 'inline' function.
19 // We exchange 2 numbers
20 // - with a macro : this is the quicker (any doubt ?)
21 // - with a function, passing the params by pointer
22 // - with a function, passing the params by reference (exactly same time)
23 // - with an inline function described in the main()
24 //                                                   absolutely NO effect ?!?
25 // - with a function, described in the .h
26 //                                                   absolutely NO effect ?!?
27 // - with an inline function, described in the .h
28 //                                                   absolutely NO effect ?!?
29 //
30 // Which CXX_FLAGS, LINKER_FLAGS, ...,  must we set to see the difference?
31
32 #include "gdcmUtil.h"
33
34 #include <sys/times.h>
35 #include <iostream>
36
37 void        frswap (double &a, double &b);
38 void        fpswap (double *a, double *b);
39 inline void ifrswap(double &a, double &b);
40 inline void ifpswap(double *a, double *b);
41
42 uint8_t     passDirect8(uint8_t a,  uint8_t b);
43 uint8_t     passRef8(uint8_t &a, uint8_t &b);
44 uint8_t     passPtr8(uint8_t *a, uint8_t *b);
45
46 uint16_t     passDirect16(uint16_t a,  uint16_t b);
47 uint16_t     passRef16(uint16_t &a, uint16_t &b);
48 uint16_t     passPtr16(uint16_t *a, uint16_t *b);
49
50 uint32_t     passDirect32(uint32_t a,  uint32_t b);
51 uint32_t     passRef32(uint32_t &a, uint32_t &b);
52 uint32_t     passPtr32(uint32_t *a, uint32_t *b);
53
54 double     passDirect(double a,  double b);
55 double     passRef(double &a, double &b);
56 double     passPtr(double *a, double *b);
57
58 #define       \
59 mswap(a, b)   \
60 {             \
61    double tmp = a;   \
62    a   = b;   \
63    b   = tmp; \
64 }
65
66 void frswap(double &a, double &b)
67 {
68    double tmp;
69    tmp = a;
70    a   = b;
71    b   = tmp;
72
73 }
74
75 void fpswap(double *a, double *b)
76 {
77    double tmp;
78    tmp = *a;
79    *a  = *b;
80    *b  = tmp;
81
82 }
83
84 inline void ifpswap(double *a, double *b)
85 {
86    double tmp;
87    tmp = *a;
88    *a  = *b;
89    *b  = tmp;
90 }
91
92 inline void ifrswap(double &a, double &b)
93 {
94    double tmp;
95    tmp = a;
96    a   = b;
97    b   = tmp;
98 }
99
100
101 uint32_t passRef32(uint32_t &a, uint32_t &b)
102 {
103    return a + b;
104
105 uint32_t passPtr32(uint32_t *a, uint32_t *b)
106 {
107    return *a + *b;
108
109 uint32_t passDirect32(uint32_t a, uint32_t b)
110 {
111    return a + b;
112
113
114
115 uint16_t passRef16(uint16_t &a, uint16_t &b)
116 {
117    return a + b;
118
119 uint16_t passPtr16(uint16_t *a, uint16_t *b)
120 {
121    return *a + *b;
122
123 uint16_t passDirect16(uint16_t a, uint16_t b)
124 {
125    return a + b;
126
127
128 uint8_t passRef8(uint8_t &a, uint8_t &b)
129 {
130    return a + b;
131
132 uint8_t passPtr8(uint8_t *a, uint8_t *b)
133 {
134    return *a + *b;
135
136 uint8_t passDirect8(uint8_t a, uint8_t b)
137 {
138    return a + b;
139
140
141 float passRefFloat(float &a, float &b)
142 {
143    return a + b;
144
145 float passPtrFloat(float *a, float *b)
146 {
147    return *a + *b;
148
149 float passDirectFloat(float a, float b)
150 {
151    return a + b;
152
153
154 double passRefDouble(double &a, double &b)
155 {
156    return a + b;
157
158 double passPtrDouble(double *a, double *b)
159 {
160    return *a + *b;
161
162 double passDirectDouble(double a, double b)
163 {
164    return a + b;
165
166
167 int TestInline(int argc, char *argv[])
168 {
169
170    unsigned int nbLoop; 
171    unsigned int i;
172       
173    if (argc > 1)
174       nbLoop = atoi(argv[1]);
175    else
176       nbLoop = 500000000;
177
178    //clock_t r1, r2;
179    struct tms tms1, tms2;
180    
181    double a = 1, b = 2;
182    
183    uint8_t  x8 =1, y8 =2;    
184    uint16_t x16=1, y16=2;    
185    uint32_t x32=1, y32=2;    
186    float  fx =1.0f, fy=1.0f;
187    double dx =1.0 , dy=1.0;
188  // ----------------------------------------
189  
190    std::cout << "Direct "<< std::endl;
191    //r1 = times(&tms1);
192    times(&tms1);   
193    for(i = 0 ; i< nbLoop ; i++)
194    {
195       double tmp;
196       tmp=a;
197       a=b;
198       b=tmp;
199    }
200    //r2 = times(&tms2);
201    times(&tms2);   
202     std::cout 
203         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
204         << std::endl;
205    
206  // ----------------------------------------
207  
208    std::cout << "Use a macro "<< std::endl;
209    //r1 = times(&tms1);
210    times(&tms1);   
211    for(i = 0 ; i< nbLoop ; i++)
212    {
213       mswap (a,b);  
214    }
215    //r2 = times(&tms2);
216    times(&tms2);   
217     std::cout 
218         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
219         << std::endl;
220    
221  // ----------------------------------------
222  
223    std::cout << "Use reference function" << std::endl;
224    //r1 = times(&tms1);
225    times(&tms1);   
226     for(i = 0 ; i< nbLoop ; i++)
227    {
228       frswap (a,b);  
229    }
230    //r2 = times(&tms2);
231    times(&tms2);   
232    std::cout 
233         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
234         << std::endl; 
235    
236  // ----------------------------------------
237   
238    std::cout << "Use pointer function" << std::endl;
239    //r1 = times(&tms1);
240    times(&tms1);   
241     for(i = 0 ; i< nbLoop ; i++)
242    {
243       fpswap (&a, &b);  
244    }
245    //r2 = times(&tms2);
246    times(&tms2);   
247    std::cout 
248         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
249         << std::endl;  
250    
251  // ----------------------------------------
252  
253    std::cout << "Use inline, main-defined reference function" << std::endl;
254    //r1 = times(&tms1);
255    times(&tms1);   
256     for(i = 0 ; i< nbLoop ; i++)
257    {
258       ifrswap (a, b);  
259    }
260    //r2 = times(&tms2);
261    times(&tms2);   
262    std::cout 
263         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
264         << std::endl;    
265    
266  // ----------------------------------------
267  
268    std::cout << "Use inline, main-defined pointer function" << std::endl;
269    //r1 = times(&tms1);
270    times(&tms1);   
271     for(i = 0 ; i< nbLoop ; i++)
272    {
273       ifpswap (&a, &b);  
274    }
275    //r2 = times(&tms2);
276    times(&tms2);   
277    std::cout 
278         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
279         << std::endl;
280
281  // ----------------------------------------
282   
283 //To check the 2 following cases, we just put the two 'static' functions
284 //hifpswap and  hNoifpswap in gdcmUtil.h
285     
286    std::cout << "Use inline, .h defined, WITH inline keyword pointer method"
287              << std::endl;
288    //r1 = times(&tms1);
289    gdcm::Util util;
290    times(&tms1);   
291     for(i = 0 ; i< nbLoop ; i++)
292    {
293       util.hifpswap (&a, &b);
294    }
295    //r2 = times(&tms2);
296    times(&tms2);   
297    std::cout 
298         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
299         << std::endl;  
300
301    
302  // ----------------------------------------
303
304    std::cout << "Use inline, .h defined, NO inline keyword pointer method"
305              << std::endl;
306    //r1 = times(&tms1);
307    times(&tms1);   
308     for(i = 0 ; i< nbLoop ; i++)
309    {
310       util.hNoifpswap (&a, &b);
311    }
312    //r2 = times(&tms2);
313    times(&tms2);   
314    std::cout 
315         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
316         << std::endl; 
317
318  // ----------------------------------------
319
320    std::cout << "Use , .h defined, NO inline keyword pointer method"
321              << std::endl;
322    //r1 = times(&tms1);
323    times(&tms1);   
324     for(i = 0 ; i< nbLoop ; i++)
325    {
326       util.hfpswap (&a, &b);
327    }
328    //r2 = times(&tms2);
329    times(&tms2);   
330    std::cout 
331         << (long) ((tms2.tms_utime)  - (tms1.tms_utime))
332         <<std::endl;
333  
334  // ----------------------------------------
335
336    std::cout << "Use inline, .h defined, WITH inline keyword pointer static method"
337              << std::endl;
338    //r1 = times(&tms1);
339    times(&tms1);   
340     for(i = 0 ; i< nbLoop ; i++)
341    {
342       gdcm::Util::sthifpswap (&a, &b);
343    }
344    //r2 = times(&tms2);
345    times(&tms2);   
346    std::cout 
347         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
348         << std::endl;  
349
350    
351  // ----------------------------------------
352
353    std::cout << "Use inline, .h defined, NO inline keyword pointer static method"
354              << std::endl;
355    //r1 = times(&tms1);
356    times(&tms1);   
357     for(i = 0 ; i< nbLoop ; i++)
358    {
359       gdcm::Util::sthNoifpswap (&a, &b);
360    }
361    //r2 = times(&tms2);
362    times(&tms2);   
363    std::cout 
364         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
365         << std::endl; 
366
367
368  // ----------------------------------------
369  
370    std::cout << "Pass uint_8 param directly"
371              << std::endl;
372    //r1 = times(&tms1);
373    times(&tms1);   
374     for(i = 0 ; i< nbLoop ; i++)
375    {
376       passDirect8 (x8, y8);  
377    }
378    //r2 = times(&tms2);
379    times(&tms2);   
380    std::cout 
381         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
382         << std::endl; 
383
384  // ----------------------------------------
385  
386    std::cout << "Pass uint_8 param as ref"
387              << std::endl;
388    //r1 = times(&tms1);
389    times(&tms1);   
390     for(i = 0 ; i< nbLoop ; i++)
391    {
392       passRef8 (x8, y8);  
393    }
394    //r2 = times(&tms2);
395    times(&tms2);   
396    std::cout 
397         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
398         << std::endl; 
399
400  // ----------------------------------------
401  
402    std::cout << "Pass uint_8 param as ptr"
403              << std::endl;
404    //r1 = times(&tms1);
405    times(&tms1);   
406     for(i = 0 ; i< nbLoop ; i++)
407    {
408       passPtr8 (&x8, &y8);  
409    }
410    //r2 = times(&tms2);
411    times(&tms2);   
412    std::cout 
413         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
414         << std::endl; 
415
416  // ----------------------------------------
417  
418    std::cout << "Pass uint_16 param directly"
419              << std::endl;
420    //r1 = times(&tms1);
421    times(&tms1);   
422     for(i = 0 ; i< nbLoop ; i++)
423    {
424       passDirect16 (x16, y16);  
425    }
426    //r2 = times(&tms2);
427    times(&tms2);   
428    std::cout 
429         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
430         << std::endl; 
431
432  // ----------------------------------------
433  
434    std::cout << "Pass uint_16 param as ref"
435              << std::endl;
436    //r1 = times(&tms1);
437    times(&tms1);   
438     for(i = 0 ; i< nbLoop ; i++)
439    {
440       passRef16 (x16, y16);  
441    }
442    //r2 = times(&tms2);
443    times(&tms2);   
444    std::cout 
445         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
446         << std::endl; 
447
448  // ----------------------------------------
449  
450    std::cout << "Pass uint_16 param as ptr"
451              << std::endl;
452    //r1 = times(&tms1);
453    times(&tms1);   
454     for(i = 0 ; i< nbLoop ; i++)
455    {
456       passPtr16 (&x16, &y16);  
457    }
458    //r2 = times(&tms2);
459    times(&tms2);   
460    std::cout 
461         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
462         << std::endl; 
463
464
465
466  // ----------------------------------------
467
468    std::cout << "Pass uint_32 param directly"
469              << std::endl;
470    //r1 = times(&tms1);
471    times(&tms1);   
472     for(i = 0 ; i< nbLoop ; i++)
473    {
474       passDirect32 (x32, y32);  
475    }
476    //r2 = times(&tms2);
477    times(&tms2);   
478    std::cout 
479         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
480         << std::endl; 
481
482  // ----------------------------------------
483  
484    std::cout << "Pass uint32_t param as ref"
485              << std::endl;
486    //r1 = times(&tms1);
487    times(&tms1);   
488     for(i = 0 ; i< nbLoop ; i++)
489    {
490       passRef32 (x32, y32 );  
491    }
492    //r2 = times(&tms2);
493    times(&tms2);   
494    std::cout 
495         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
496         << std::endl; 
497
498  // ----------------------------------------
499  
500    std::cout << "Pass uint_32 param as ptr"
501              << std::endl;
502    //r1 = times(&tms1);
503    times(&tms1);   
504     for(i = 0 ; i< nbLoop ; i++)
505    {
506       passPtr32 (&x32, &y32);  
507    }
508    //r2 = times(&tms2);
509    times(&tms2);   
510    std::cout 
511         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
512         << std::endl; 
513
514  // ----------------------------------------
515  
516    std::cout << "Pass float param directly"
517              << std::endl;
518    //r1 = times(&tms1);
519    times(&tms1);   
520     for(i = 0 ; i< nbLoop ; i++)
521    {
522       passDirectFloat (fx, fy);  
523    }
524    //r2 = times(&tms2);
525    times(&tms2);   
526
527    std::cout 
528         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
529         << std::endl; 
530
531  // ----------------------------------------
532  
533    std::cout << "Pass float param as ref"
534              << std::endl;
535    //r1 = times(&tms1);
536    times(&tms1);   
537     for(i = 0 ; i< nbLoop ; i++)
538    {
539       passRefFloat (fx, fy);  
540    }
541    //r2 = times(&tms2);
542    times(&tms2);   
543
544    std::cout 
545         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
546         << std::endl; 
547
548  // ----------------------------------------
549  
550    std::cout << "Pass float param as ptr"
551              << std::endl;
552    //r1 = times(&tms1);
553    times(&tms1);   
554     for(i = 0 ; i< nbLoop ; i++)
555    {
556       passPtrFloat (&fx, &fy);  
557    }
558    //r2 = times(&tms2);
559    times(&tms2);   
560
561    std::cout 
562         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
563         << std::endl; 
564
565  // ----------------------------------------
566  
567    std::cout << "Pass double param directly"
568              << std::endl;
569    //r1 = times(&tms1);
570    times(&tms1);   
571     for(i = 0 ; i< nbLoop ; i++)
572    {
573       passDirectDouble (dx, dy);  
574    }
575    //r2 = times(&tms2);
576    times(&tms2);   
577
578    std::cout 
579         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
580         << std::endl; 
581
582  // ----------------------------------------
583  
584    std::cout << "Pass double param as ref"
585              << std::endl;
586    //r1 = times(&tms1);
587    times(&tms1);   
588     for(i = 0 ; i< nbLoop ; i++)
589    {
590       passRefDouble (dx, dy);  
591    }
592    //r2 = times(&tms2);
593    times(&tms2);   
594
595    std::cout 
596         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
597         << std::endl; 
598
599  // ----------------------------------------
600  
601    std::cout << "Pass double param as ptr"
602              << std::endl;
603    //r1 = times(&tms1);
604    times(&tms1);   
605     for(i = 0 ; i< nbLoop ; i++)
606    {
607       passPtrDouble (&dx, &dy);  
608    }
609    //r2 = times(&tms2);
610    times(&tms2);   
611
612    std::cout 
613         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
614         << std::endl; 
615
616    return 0;
617 }