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