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