]> Creatis software - gdcm.git/blob - Testing/TestInline.cxx
Re-activate TestInLine
[gdcm.git] / Testing / TestInline.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestInline.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/09/20 12:48:21 $
7   Version:   $Revision: 1.12 $
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    std::cout << std::endl << std::endl
170              << "Just to be sure : sizes of native types" 
171              << std::endl << std::endl;
172    // just to know, on every proc
173    std::cout << "Size of short int " << sizeof(short int) << std::endl;
174    std::cout << "Size of int  "      << sizeof(int)       << std::endl;
175    std::cout << "Size of long "      << sizeof(long)      << std::endl;
176    std::cout << "Size of float"      << sizeof(float)     << std::endl;
177    std::cout << "Size of double"     << sizeof(double)    << std::endl;
178    std::cout << "Size of int* "      << sizeof(int*)      << std::endl;
179    std::cout <<  "-----------------" << std::endl;
180    unsigned int nbLoop; 
181    unsigned int i;
182       
183    if (argc > 1)
184       nbLoop = atoi(argv[1]);
185    else
186       nbLoop = 100000000;
187
188    //clock_t r1, r2;
189    struct tms tms1, tms2;
190    
191    double a = 1, b = 2;
192    
193    std::cout << "Exchange 2 scalars " << nbLoop << " times"
194              << std::endl << std::endl;
195    uint8_t  x8 =1, y8 =2;    
196    uint16_t x16=1, y16=2;    
197    uint32_t x32=1, y32=2;    
198    float  fx =1.0f, fy=1.0f;
199    double dx =1.0 , dy=1.0;
200  // ----------------------------------------
201  
202    std::cout << "Direct "<< std::endl;
203    //r1 = times(&tms1);
204    times(&tms1);   
205    for(i = 0 ; i< nbLoop ; i++)
206    {
207       double tmp;
208       tmp=a;
209       a=b;
210       b=tmp;
211    }
212    //r2 = times(&tms2);
213    times(&tms2);   
214     std::cout 
215         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
216         << std::endl;
217    
218  // ----------------------------------------
219  
220    std::cout << "Use a macro "<< std::endl;
221    //r1 = times(&tms1);
222    times(&tms1);   
223    for(i = 0 ; i< nbLoop ; i++)
224    {
225       mswap (a,b);  
226    }
227    //r2 = times(&tms2);
228    times(&tms2);   
229     std::cout 
230         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
231         << std::endl;
232    
233  // ----------------------------------------
234  
235    std::cout << "Use reference function" << std::endl;
236    //r1 = times(&tms1);
237    times(&tms1);   
238     for(i = 0 ; i< nbLoop ; i++)
239    {
240       frswap (a,b);  
241    }
242    //r2 = times(&tms2);
243    times(&tms2);   
244    std::cout 
245         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
246         << std::endl; 
247    
248  // ----------------------------------------
249   
250    std::cout << "Use pointer function" << std::endl;
251    //r1 = times(&tms1);
252    times(&tms1);   
253     for(i = 0 ; i< nbLoop ; i++)
254    {
255       fpswap (&a, &b);  
256    }
257    //r2 = times(&tms2);
258    times(&tms2);   
259    std::cout 
260         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
261         << std::endl;  
262    
263  // ----------------------------------------
264  
265    std::cout << "Use inline, main-defined reference function" << std::endl;
266    //r1 = times(&tms1);
267    times(&tms1);   
268     for(i = 0 ; i< nbLoop ; i++)
269    {
270       ifrswap (a, b);  
271    }
272    //r2 = times(&tms2);
273    times(&tms2);   
274    std::cout 
275         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
276         << std::endl;    
277    
278  // ----------------------------------------
279  
280    std::cout << "Use inline, main-defined pointer function" << std::endl;
281    //r1 = times(&tms1);
282    times(&tms1);   
283     for(i = 0 ; i< nbLoop ; i++)
284    {
285       ifpswap (&a, &b);  
286    }
287    //r2 = times(&tms2);
288    times(&tms2);   
289    std::cout 
290         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
291         << std::endl;
292
293  // ----------------------------------------
294   
295 //To check the 2 following cases, we just put the two 'static' functions
296 //hifpswap and  hNoifpswap in gdcmUtil.h
297     
298    std::cout << "Use inline, .h defined, WITH inline keyword pointer method"
299              << std::endl;
300    //r1 = times(&tms1);
301    gdcm::Util util;
302    times(&tms1);   
303     for(i = 0 ; i< nbLoop ; i++)
304    {
305       util.hifpswap (&a, &b);
306    }
307    //r2 = times(&tms2);
308    times(&tms2);   
309    std::cout 
310         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
311         << std::endl;  
312
313    
314  // ----------------------------------------
315
316    std::cout << "Use inline, .h defined, NO inline keyword pointer method"
317              << std::endl;
318    //r1 = times(&tms1);
319    times(&tms1);   
320     for(i = 0 ; i< nbLoop ; i++)
321    {
322       util.hNoifpswap (&a, &b);
323    }
324    //r2 = times(&tms2);
325    times(&tms2);   
326    std::cout 
327         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
328         << std::endl; 
329
330  // ----------------------------------------
331
332    std::cout << "Use , .h defined, NO inline keyword pointer method"
333              << std::endl;
334    //r1 = times(&tms1);
335    times(&tms1);   
336     for(i = 0 ; i< nbLoop ; i++)
337    {
338       util.hfpswap (&a, &b);
339    }
340    //r2 = times(&tms2);
341    times(&tms2);   
342    std::cout 
343         << (long) ((tms2.tms_utime)  - (tms1.tms_utime))
344         <<std::endl;
345  
346  // ----------------------------------------
347
348    std::cout << "Use inline, .h defined, WITH inline keyword pointer static method"
349              << std::endl;
350    //r1 = times(&tms1);
351    times(&tms1);   
352     for(i = 0 ; i< nbLoop ; i++)
353    {
354       gdcm::Util::sthifpswap (&a, &b);
355    }
356    //r2 = times(&tms2);
357    times(&tms2);   
358    std::cout 
359         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
360         << std::endl;  
361
362    
363  // ----------------------------------------
364
365    std::cout << "Use inline, .h defined, NO inline keyword pointer static method"
366              << std::endl;
367    //r1 = times(&tms1);
368    times(&tms1);   
369     for(i = 0 ; i< nbLoop ; i++)
370    {
371       gdcm::Util::sthNoifpswap (&a, &b);
372    }
373    //r2 = times(&tms2);
374    times(&tms2);   
375    std::cout 
376         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
377         << std::endl; 
378
379
380  // ----------------------------------------
381    std::cout << std::endl << std::endl
382              << "Check different ways of passing scalars to a function, " 
383              << nbLoop << " times" << std::endl << std::endl; 
384     
385    std::cout << "Pass uint_8 param directly"
386              << std::endl;
387    //r1 = times(&tms1);
388    times(&tms1);   
389     for(i = 0 ; i< nbLoop ; i++)
390    {
391       passDirect8 (x8, y8);  
392    }
393    //r2 = times(&tms2);
394    times(&tms2);   
395    std::cout 
396         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
397         << std::endl; 
398
399  // ----------------------------------------
400  
401    std::cout << "Pass uint_8 param as ref"
402              << std::endl;
403    //r1 = times(&tms1);
404    times(&tms1);   
405     for(i = 0 ; i< nbLoop ; i++)
406    {
407       passRef8 (x8, y8);  
408    }
409    //r2 = times(&tms2);
410    times(&tms2);   
411    std::cout 
412         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
413         << std::endl; 
414
415  // ----------------------------------------
416  
417    std::cout << "Pass uint_8 param as ptr"
418              << std::endl;
419    //r1 = times(&tms1);
420    times(&tms1);   
421     for(i = 0 ; i< nbLoop ; i++)
422    {
423       passPtr8 (&x8, &y8);  
424    }
425    //r2 = times(&tms2);
426    times(&tms2);   
427    std::cout 
428         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
429         << std::endl; 
430
431  // ----------------------------------------
432    std::cout << std::endl;
433    std::cout << "Pass uint_16 param directly"
434              << std::endl;
435    //r1 = times(&tms1);
436    times(&tms1);   
437     for(i = 0 ; i< nbLoop ; i++)
438    {
439       passDirect16 (x16, y16);  
440    }
441    //r2 = times(&tms2);
442    times(&tms2);   
443    std::cout 
444         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
445         << std::endl; 
446
447  // ----------------------------------------
448  
449    std::cout << "Pass uint_16 param as ref"
450              << std::endl;
451    //r1 = times(&tms1);
452    times(&tms1);   
453     for(i = 0 ; i< nbLoop ; i++)
454    {
455       passRef16 (x16, y16);  
456    }
457    //r2 = times(&tms2);
458    times(&tms2);   
459    std::cout 
460         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
461         << std::endl; 
462
463  // ----------------------------------------
464  
465    std::cout << "Pass uint_16 param as ptr"
466              << std::endl;
467    //r1 = times(&tms1);
468    times(&tms1);   
469     for(i = 0 ; i< nbLoop ; i++)
470    {
471       passPtr16 (&x16, &y16);  
472    }
473    //r2 = times(&tms2);
474    times(&tms2);   
475    std::cout 
476         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
477         << std::endl; 
478
479
480
481  // ----------------------------------------
482    std::cout << std::endl;
483    std::cout << "Pass uint_32 param directly"
484              << std::endl;
485    //r1 = times(&tms1);
486    times(&tms1);   
487     for(i = 0 ; i< nbLoop ; i++)
488    {
489       passDirect32 (x32, y32);  
490    }
491    //r2 = times(&tms2);
492    times(&tms2);   
493    std::cout 
494         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
495         << std::endl; 
496
497  // ----------------------------------------
498  
499    std::cout << "Pass uint32_t param as ref"
500              << std::endl;
501    //r1 = times(&tms1);
502    times(&tms1);   
503     for(i = 0 ; i< nbLoop ; i++)
504    {
505       passRef32 (x32, y32 );  
506    }
507    //r2 = times(&tms2);
508    times(&tms2);   
509    std::cout 
510         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
511         << std::endl; 
512
513  // ----------------------------------------
514  
515    std::cout << "Pass uint_32 param as ptr"
516              << std::endl;
517    //r1 = times(&tms1);
518    times(&tms1);   
519     for(i = 0 ; i< nbLoop ; i++)
520    {
521       passPtr32 (&x32, &y32);  
522    }
523    //r2 = times(&tms2);
524    times(&tms2);   
525    std::cout 
526         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
527         << std::endl; 
528
529  // ----------------------------------------
530    std::cout << std::endl; 
531    std::cout << "Pass float param directly"
532              << std::endl;
533    //r1 = times(&tms1);
534    times(&tms1);   
535     for(i = 0 ; i< nbLoop ; i++)
536    {
537       passDirectFloat (fx, fy);  
538    }
539    //r2 = times(&tms2);
540    times(&tms2);   
541
542    std::cout 
543         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
544         << std::endl; 
545
546  // ----------------------------------------
547  
548    std::cout << "Pass float param as ref"
549              << std::endl;
550    //r1 = times(&tms1);
551    times(&tms1);   
552     for(i = 0 ; i< nbLoop ; i++)
553    {
554       passRefFloat (fx, fy);  
555    }
556    //r2 = times(&tms2);
557    times(&tms2);   
558
559    std::cout 
560         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
561         << std::endl; 
562
563  // ----------------------------------------
564  
565    std::cout << "Pass float param as ptr"
566              << std::endl;
567    //r1 = times(&tms1);
568    times(&tms1);   
569     for(i = 0 ; i< nbLoop ; i++)
570    {
571       passPtrFloat (&fx, &fy);  
572    }
573    //r2 = times(&tms2);
574    times(&tms2);   
575
576    std::cout 
577         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
578         << std::endl; 
579
580  // ----------------------------------------
581    std::cout << std::endl; 
582    std::cout << "Pass double param directly"
583              << std::endl;
584    //r1 = times(&tms1);
585    times(&tms1);   
586     for(i = 0 ; i< nbLoop ; i++)
587    {
588       passDirectDouble (dx, dy);  
589    }
590    //r2 = times(&tms2);
591    times(&tms2);   
592
593    std::cout 
594         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
595         << std::endl; 
596
597  // ----------------------------------------
598  
599    std::cout << "Pass double param as ref"
600              << std::endl;
601    //r1 = times(&tms1);
602    times(&tms1);   
603     for(i = 0 ; i< nbLoop ; i++)
604    {
605       passRefDouble (dx, dy);  
606    }
607    //r2 = times(&tms2);
608    times(&tms2);   
609
610    std::cout 
611         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
612         << std::endl; 
613
614  // ----------------------------------------
615  
616    std::cout << "Pass double param as ptr"
617              << std::endl;
618    //r1 = times(&tms1);
619    times(&tms1);   
620     for(i = 0 ; i< nbLoop ; i++)
621    {
622       passPtrDouble (&dx, &dy);  
623    }
624    //r2 = times(&tms2);
625    times(&tms2);   
626
627    std::cout 
628         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
629         << std::endl; 
630
631    return 0;
632 }