]> Creatis software - gdcm.git/blob - Example/exInline.cxx
Fix mistypings
[gdcm.git] / Example / exInline.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exInline.cxx,v $
5   Language:  C++
6   Date:      $Date: 2007/06/21 15:01:00 $
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 (anny 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() : absolutely NO effect ?!?
24 // - with an inline function described in the .h     : absolutely NO effect ?!?
25 //
26 // Must we ask optimization to see the difference?
27
28 #include "gdcmUtil.h"
29 #include "gdcmDebug.h"
30 #include <iostream>
31
32 #include <time.h>
33 #include <sys/times.h>
34
35 void        frswap (double &a, double &b);
36 void        fpswap (double *a, double *b);
37 inline void ifrswap(double &a, double &b);
38 inline void ifpswap(double *a, double *b);
39
40
41 #define       \
42 mswap(a, b)   \
43 {             \
44    tmp = a;   \
45    a   = b;   \
46    b   = tmp; \
47 }
48
49 void frswap(double &a, double &b)
50 {
51    double tmp;
52    tmp = a;
53    a   = b;
54    b   = tmp;
55
56 }
57
58 void fpswap(double *a, double *b)
59 {
60    double tmp;
61    tmp = *a;
62    *a  = *b;
63    *b  = tmp;
64
65 }
66
67 inline void ifpswap(double *a, double *b)
68 {
69    double tmp;
70    tmp = *a;
71    *a  = *b;
72    *b  = tmp;
73 }
74
75 inline void ifrswap(double &a, double &b)
76 {
77    double tmp;
78    tmp = a;
79    a   = b;
80    b   = tmp;
81 }
82
83 int main(int argc, char *argv[])
84 {
85
86 uint32_t a1 = 0xfedcba98;
87 uint64_t b1 = a1<<8;
88 std::cout<<  "sizeof(uint32_t) " << sizeof(uint32_t) 
89          << " sizeof(uint64_t) " << sizeof(uint64_t) << std::endl;
90
91 std::cout<< std::hex <<a1 << " " << b1  << std::endl;
92 b1 = 0xfedcba98;
93 uint64_t b2= 0x76543210;
94 b1= b1<<32|b2;
95 std::cout<< std::hex <<b1 << " " << b2  << std::endl;
96
97    int nbLoop;  
98    if (argc > 1)
99       nbLoop = atoi(argv[1]);
100    else
101       nbLoop = 100000000;      
102    unsigned int i;
103    clock_t r1, r2;
104    struct tms tms1, tms2;
105    
106    double a = 1, b = 2;
107    double tmp; 
108  // ----------------------------------------
109  
110    std::cout << "Use a macro "<< std::endl;
111    r1 = times(&tms1);   
112    for(i = 0 ; i< nbLoop ; i++)
113    {
114       mswap (a,b);  
115    }
116    r2 = times(&tms2);
117    std::cout 
118         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
119         << std::endl;
120    
121  // ----------------------------------------
122  
123    std::cout << "Use reference function" << std::endl;
124    r1 = times(&tms1);         
125    for(i = 0 ; i< nbLoop ; i++)
126    {
127       frswap (a,b);  
128    }
129    r2 = times(&tms2);
130    std::cout 
131         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
132         << std::endl; 
133    
134  // ----------------------------------------
135   
136    std::cout << "Use pointer function" << std::endl;
137    r1 = times(&tms1);     
138    for(i = 0 ; i< nbLoop ; i++)
139    {
140       fpswap (&a, &b);  
141    }
142    r2 = times(&tms2);
143    std::cout 
144         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
145         << std::endl;  
146    
147  // ----------------------------------------
148  
149    std::cout << "Use inline, main-defined reference function" << std::endl;
150    r1 = times(&tms1);     
151    for(i = 0 ; i< nbLoop ; i++)
152    {
153       ifrswap (a, b);  
154    }
155    r2 = times(&tms2);
156    std::cout 
157         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
158         << std::endl;    
159    
160  // ----------------------------------------
161  
162    std::cout << "Use inline, main-defined pointer function" << std::endl;
163    r1 = times(&tms1);     
164    for(i = 0 ; i< nbLoop ; i++)
165    {
166       ifpswap (&a, &b);  
167    }
168    r2 = times(&tms2);
169    std::cout 
170         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
171         << std::endl;
172
173 /*  
174 //To check the 2 following cases, just put the two 'static' functions
175 //hifpswap and  hNoifpswap in a .h
176
177    static inline void hifpswap(double *a, double *b)    
178    {
179       double tmp;
180       tmp = *a;
181       *a = *b;
182       *b = tmp;
183    }
184
185    static void hNoifpswap(double *a, double *b)    
186    {
187       double tmp;
188       tmp = *a;
189       *a = *b;
190       *b = tmp;
191    }
192     
193  // ----------------------------------------
194     
195    std::cout << "Use inline, .h defined, WITH inline keyword pointer function"
196              << std::endl;
197    r1 = times(&tms1);     
198    for(i = 0 ; i< nbLoop ; i++)
199    {
200       GDCM_NAME_SPACE::Util::hifpswap (&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
210    std::cout << "Use inline, .h defined, NO inline keyword pointer function"
211              << std::endl;
212    r1 = times(&tms1);     
213    for(i = 0 ; i< nbLoop ; i++)
214    {
215       GDCM_NAME_SPACE::Util::hNoifpswap (&a, &b);  
216    }
217    r2 = times(&tms2);
218    std::cout 
219         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
220         << std::endl; 
221 */ 
222
223 }