]> Creatis software - gdcm.git/blob - Testing/TestInline.cxx
Avoid warning with icc
[gdcm.git] / Testing / TestInline.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: TestInline.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/11/17 17:44:24 $
7   Version:   $Revision: 1.8 $
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 #include "TestInLine.h"
34 #include <iostream>
35
36 #include <time.h>
37 #ifdef CMAKE_HAVE_SYS_TIMES_H
38 #include <sys/times.h>
39 #else
40 #include <sys/timeb.h>
41 #endif
42
43 void        frswap (double &a, double &b);
44 void        fpswap (double *a, double *b);
45 inline void ifrswap(double &a, double &b);
46 inline void ifpswap(double *a, double *b);
47
48 uint16_t     passDirect(uint16_t a,  uint16_t b);
49 uint16_t     passRef(uint16_t &a, uint16_t &b);
50 uint16_t     passPtr(uint16_t *a, uint16_t *b);
51
52 uint32_t     passDirect32(uint32_t a,  uint32_t b);
53 uint32_t     passRef32(uint32_t &a, uint32_t &b);
54 uint32_t     passPtr32(uint32_t *a, uint32_t *b);
55
56 double     passDirect(double a,  double b);
57 double     passRef(double &a, double &b);
58 double     passPtr(double *a, double *b);
59
60 #define       \
61 mswap(a, b)   \
62 {             \
63    tmp = a;   \
64    a   = b;   \
65    b   = tmp; \
66 }
67
68 void frswap(double &a, double &b)
69 {
70    double tmp;
71    tmp = a;
72    a   = b;
73    b   = tmp;
74
75 }
76
77 void fpswap(double *a, double *b)
78 {
79    double tmp;
80    tmp = *a;
81    *a  = *b;
82    *b  = tmp;
83
84 }
85
86 inline void ifpswap(double *a, double *b)
87 {
88    double tmp;
89    tmp = *a;
90    *a  = *b;
91    *b  = tmp;
92 }
93
94 inline void ifrswap(double &a, double &b)
95 {
96    double tmp;
97    tmp = a;
98    a   = b;
99    b   = tmp;
100 }
101
102
103 uint32_t passRef32(uint32_t &a, uint32_t &b)
104 {
105    return a + b;
106
107 uint32_t passPtr32(uint32_t *a, uint32_t *b)
108 {
109    return *a + *b;
110
111 uint32_t passDirect32(uint32_t a, uint32_t b)
112 {
113    return a + b;
114
115
116
117 uint16_t passRef(uint16_t &a, uint16_t &b)
118 {
119    return a + b;
120
121 uint16_t passPtr(uint16_t *a, uint16_t *b)
122 {
123    return *a + *b;
124
125 uint16_t passDirect(uint16_t a, uint16_t b)
126 {
127    return a + b;
128
129
130 double passRefDouble(double &a, double &b)
131 {
132    return a + b;
133
134 double passPtrDouble(double *a, double *b)
135 {
136    return *a + *b;
137
138 double passDirectDouble(double a, double b)
139 {
140    return a + b;
141
142
143 int TestInline(int argc, char *argv[])
144 {
145
146    unsigned int nbLoop; 
147    unsigned int i;
148       
149    if (argc > 1)
150       nbLoop = atoi(argv[1]);
151    else
152       nbLoop = 10000000;      
153
154    //clock_t r1, r2;
155    struct tms tms1, tms2;
156    
157    double a = 1, b = 2;
158    double tmp;
159    
160    uint16_t x=1, y=2;    
161  // ----------------------------------------
162  
163    std::cout << "Use a macro "<< std::endl;
164    //r1 = times(&tms1);
165    times(&tms1);   
166    for(i = 0 ; i< nbLoop ; i++)
167    {
168       mswap (a,b);  
169    }
170    //r2 = times(&tms2);
171    times(&tms2);   
172     std::cout 
173         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
174         << std::endl;
175    
176  // ----------------------------------------
177  
178    std::cout << "Use reference function" << std::endl;
179    //r1 = times(&tms1);
180    times(&tms1);   
181     for(i = 0 ; i< nbLoop ; i++)
182    {
183       frswap (a,b);  
184    }
185    //r2 = times(&tms2);
186    times(&tms2);   
187    std::cout 
188         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
189         << std::endl; 
190    
191  // ----------------------------------------
192   
193    std::cout << "Use pointer function" << std::endl;
194    //r1 = times(&tms1);
195    times(&tms1);   
196     for(i = 0 ; i< nbLoop ; i++)
197    {
198       fpswap (&a, &b);  
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 inline, main-defined reference function" << std::endl;
209    //r1 = times(&tms1);
210    times(&tms1);   
211     for(i = 0 ; i< nbLoop ; i++)
212    {
213       ifrswap (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 inline, main-defined pointer function" << std::endl;
224    //r1 = times(&tms1);
225    times(&tms1);   
226     for(i = 0 ; i< nbLoop ; i++)
227    {
228       ifpswap (&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 //To check the 2 following cases, we just put the two 'static' functions
239 //hifpswap and  hNoifpswap in gdcmUtil.h
240     
241    std::cout << "Use inline, .h defined, WITH inline keyword pointer function"
242              << std::endl;
243    //r1 = times(&tms1);
244    times(&tms1);   
245     for(i = 0 ; i< nbLoop ; i++)
246    {
247       //gdcm::Util::hifpswap (&a, &b);
248       hifpswap (&a, &b);  
249    }
250    //r2 = times(&tms2);
251    times(&tms2);   
252    std::cout 
253         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
254         << std::endl;  
255
256    
257  // ----------------------------------------
258
259    std::cout << "Use inline, .h defined, NO inline keyword pointer function"
260              << std::endl;
261    //r1 = times(&tms1);
262    times(&tms1);   
263     for(i = 0 ; i< nbLoop ; i++)
264    {
265       //gdcm::Util::hNoifpswap (&a, &b);
266       hNoifpswap (&a, &b);  
267    }
268    //r2 = times(&tms2);
269    times(&tms2);   
270    std::cout 
271         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
272         << std::endl; 
273
274
275  // ----------------------------------------
276  
277    std::cout << "Pass uint_16 param directly"
278              << std::endl;
279    //r1 = times(&tms1);
280    times(&tms1);   
281     for(i = 0 ; i< nbLoop ; i++)
282    {
283       passDirect (x, y);  
284    }
285    //r2 = times(&tms2);
286    times(&tms2);   
287    std::cout 
288         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
289         << std::endl; 
290
291  // ----------------------------------------
292  
293    std::cout << "Pass uint_16 param as ref"
294              << std::endl;
295    //r1 = times(&tms1);
296    times(&tms1);   
297     for(i = 0 ; i< nbLoop ; i++)
298    {
299       passRef (x, y);  
300    }
301    //r2 = times(&tms2);
302    times(&tms2);   
303    std::cout 
304         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
305         << std::endl; 
306
307  // ----------------------------------------
308  
309    std::cout << "Pass uint_16 param as ptr"
310              << std::endl;
311    //r1 = times(&tms1);
312    times(&tms1);   
313     for(i = 0 ; i< nbLoop ; i++)
314    {
315       passPtr (&x, &y);  
316    }
317    //r2 = times(&tms2);
318    times(&tms2);   
319    std::cout 
320         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
321         << std::endl; 
322
323
324
325  // ----------------------------------------
326
327    uint32_t m =1, n=2; 
328    std::cout << "Pass uint_32 param directly"
329              << std::endl;
330    //r1 = times(&tms1);
331    times(&tms1);   
332     for(i = 0 ; i< nbLoop ; i++)
333    {
334       passDirect32 (m, n);  
335    }
336    //r2 = times(&tms2);
337    times(&tms2);   
338    std::cout 
339         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
340         << std::endl; 
341
342  // ----------------------------------------
343  
344    std::cout << "Pass uint32_t param as ref"
345              << std::endl;
346    //r1 = times(&tms1);
347    times(&tms1);   
348     for(i = 0 ; i< nbLoop ; i++)
349    {
350       passRef32 (m, n);  
351    }
352    //r2 = times(&tms2);
353    times(&tms2);   
354    std::cout 
355         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
356         << std::endl; 
357
358  // ----------------------------------------
359  
360    std::cout << "Pass uint_32 param as ptr"
361              << std::endl;
362    //r1 = times(&tms1);
363    times(&tms1);   
364     for(i = 0 ; i< nbLoop ; i++)
365    {
366       passPtr32 (&m, &n);  
367    }
368    //r2 = times(&tms2);
369    times(&tms2);   
370    std::cout 
371         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
372         << std::endl; 
373
374  // ----------------------------------------
375  
376  
377    double dx=1.0, dy=2.0;
378    
379    std::cout << "Pass double param directly"
380              << std::endl;
381    //r1 = times(&tms1);
382    times(&tms1);   
383     for(i = 0 ; i< nbLoop ; i++)
384    {
385       passDirectDouble (dx, dy);  
386    }
387    //r2 = times(&tms2);
388    times(&tms2);   
389
390    std::cout 
391         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
392         << std::endl; 
393
394  // ----------------------------------------
395  
396    std::cout << "Pass double param as ref"
397              << std::endl;
398    //r1 = times(&tms1);
399    times(&tms1);   
400     for(i = 0 ; i< nbLoop ; i++)
401    {
402       passRefDouble (dx, dy);  
403    }
404    //r2 = times(&tms2);
405    times(&tms2);   
406
407    std::cout 
408         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
409         << std::endl; 
410
411  // ----------------------------------------
412  
413    std::cout << "Pass double param as ptr"
414              << std::endl;
415    //r1 = times(&tms1);
416    times(&tms1);   
417     for(i = 0 ; i< nbLoop ; i++)
418    {
419       passPtrDouble (&dx, &dy);  
420    }
421    //r2 = times(&tms2);
422    times(&tms2);   
423
424    std::cout 
425         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
426         << std::endl; 
427
428    return 0;
429 }