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