]> Creatis software - gdcm.git/blob - Testing/TestInline.cxx
* Change the DocEntry inheritance to RefCounter
[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 16:00:46 $
7   Version:   $Revision: 1.5 $
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 #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 = 100000000;      
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    for(i = 0 ; i< nbLoop ; i++)
166    {
167       mswap (a,b);  
168    }
169    r2 = times(&tms2);
170    std::cout 
171         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
172         << std::endl;
173    
174  // ----------------------------------------
175  
176    std::cout << "Use reference function" << std::endl;
177    r1 = times(&tms1);         
178    for(i = 0 ; i< nbLoop ; i++)
179    {
180       frswap (a,b);  
181    }
182    r2 = times(&tms2);
183    std::cout 
184         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
185         << std::endl; 
186    
187  // ----------------------------------------
188   
189    std::cout << "Use pointer function" << std::endl;
190    r1 = times(&tms1);     
191    for(i = 0 ; i< nbLoop ; i++)
192    {
193       fpswap (&a, &b);  
194    }
195    r2 = times(&tms2);
196    std::cout 
197         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
198         << std::endl;  
199    
200  // ----------------------------------------
201  
202    std::cout << "Use inline, main-defined reference function" << std::endl;
203    r1 = times(&tms1);     
204    for(i = 0 ; i< nbLoop ; i++)
205    {
206       ifrswap (a, b);  
207    }
208    r2 = times(&tms2);
209    std::cout 
210         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
211         << std::endl;    
212    
213  // ----------------------------------------
214  
215    std::cout << "Use inline, main-defined pointer function" << std::endl;
216    r1 = times(&tms1);     
217    for(i = 0 ; i< nbLoop ; i++)
218    {
219       ifpswap (&a, &b);  
220    }
221    r2 = times(&tms2);
222    std::cout 
223         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
224         << std::endl;
225
226  // ----------------------------------------
227   
228 //To check the 2 following cases, we just put the two 'static' functions
229 //hifpswap and  hNoifpswap in gdcmUtil.h
230     
231    std::cout << "Use inline, .h defined, WITH inline keyword pointer function"
232              << std::endl;
233    r1 = times(&tms1);     
234    for(i = 0 ; i< nbLoop ; i++)
235    {
236       gdcm::Util::hifpswap (&a, &b);  
237    }
238    r2 = times(&tms2);
239    std::cout 
240         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
241         << std::endl;  
242
243    
244  // ----------------------------------------
245
246    std::cout << "Use inline, .h defined, NO inline keyword pointer function"
247              << std::endl;
248    r1 = times(&tms1);     
249    for(i = 0 ; i< nbLoop ; i++)
250    {
251       gdcm::Util::hNoifpswap (&a, &b);  
252    }
253    r2 = times(&tms2);
254    std::cout 
255         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
256         << std::endl; 
257
258
259  // ----------------------------------------
260  
261    std::cout << "Pass uint_16 param directly"
262              << std::endl;
263    r1 = times(&tms1);     
264    for(i = 0 ; i< nbLoop ; i++)
265    {
266       passDirect (x, y);  
267    }
268    r2 = times(&tms2);
269    std::cout 
270         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
271         << std::endl; 
272
273  // ----------------------------------------
274  
275    std::cout << "Pass uint_16 param as ref"
276              << std::endl;
277    r1 = times(&tms1);     
278    for(i = 0 ; i< nbLoop ; i++)
279    {
280       passRef (x, y);  
281    }
282    r2 = times(&tms2);
283    std::cout 
284         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
285         << std::endl; 
286
287  // ----------------------------------------
288  
289    std::cout << "Pass uint_16 param as ptr"
290              << std::endl;
291    r1 = times(&tms1);     
292    for(i = 0 ; i< nbLoop ; i++)
293    {
294       passPtr (&x, &y);  
295    }
296    r2 = times(&tms2);
297    std::cout 
298         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
299         << std::endl; 
300
301
302
303  // ----------------------------------------
304
305    uint32_t m =1, n=2; 
306    std::cout << "Pass uint_32 param directly"
307              << std::endl;
308    r1 = times(&tms1);     
309    for(i = 0 ; i< nbLoop ; i++)
310    {
311       passDirect32 (m, n);  
312    }
313    r2 = times(&tms2);
314    std::cout 
315         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
316         << std::endl; 
317
318  // ----------------------------------------
319  
320    std::cout << "Pass uint32_t param as ref"
321              << std::endl;
322    r1 = times(&tms1);     
323    for(i = 0 ; i< nbLoop ; i++)
324    {
325       passRef32 (m, n);  
326    }
327    r2 = times(&tms2);
328    std::cout 
329         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
330         << std::endl; 
331
332  // ----------------------------------------
333  
334    std::cout << "Pass uint_32 param as ptr"
335              << std::endl;
336    r1 = times(&tms1);     
337    for(i = 0 ; i< nbLoop ; i++)
338    {
339       passPtr32 (&m, &n);  
340    }
341    r2 = times(&tms2);
342    std::cout 
343         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
344         << std::endl; 
345
346  // ----------------------------------------
347  
348  
349    double dx=1.0, dy=2.0;
350    
351    std::cout << "Pass double param directly"
352              << std::endl;
353    r1 = times(&tms1);     
354    for(i = 0 ; i< nbLoop ; i++)
355    {
356       passDirectDouble (dx, dy);  
357    }
358    r2 = times(&tms2);
359    std::cout 
360         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
361         << std::endl; 
362
363  // ----------------------------------------
364  
365    std::cout << "Pass double param as ref"
366              << std::endl;
367    r1 = times(&tms1);     
368    for(i = 0 ; i< nbLoop ; i++)
369    {
370       passRefDouble (dx, dy);  
371    }
372    r2 = times(&tms2);
373    std::cout 
374         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
375         << std::endl; 
376
377  // ----------------------------------------
378  
379    std::cout << "Pass double param as ptr"
380              << std::endl;
381    r1 = times(&tms1);     
382    for(i = 0 ; i< nbLoop ; i++)
383    {
384       passPtrDouble (&dx, &dy);  
385    }
386    r2 = times(&tms2);
387    std::cout 
388         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
389         << std::endl; 
390
391    return 0;
392 }
393 */
394
395 int TestInline(int argc, char *argv[])
396 {
397    return 0;
398 }