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