]> Creatis software - gdcm.git/blob - Example/exInline.cxx
Add exInline.cxx, to 'visualize' the difference betwwen inline an not inline
[gdcm.git] / Example / exInline.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: exInline.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/10/21 08:43:31 $
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 (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    int nbLoop;  
87    if (argc > 1)
88       nbLoop = atoi(argv[1]);
89    else
90       nbLoop = 100000000;      
91    unsigned int i;
92    clock_t r1, r2;
93    struct tms tms1, tms2;
94    
95    double a = 1, b = 2;
96    double tmp; 
97  // ----------------------------------------
98  
99    std::cout << "Use a macro "<< std::endl;
100    r1 = times(&tms1);   
101    for(i = 0 ; i< nbLoop ; i++)
102    {
103       mswap (a,b);  
104    }
105    r2 = times(&tms2);
106    std::cout 
107         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
108         << std::endl;
109    
110  // ----------------------------------------
111  
112    std::cout << "Use reference function" << std::endl;
113    r1 = times(&tms1);         
114    for(i = 0 ; i< nbLoop ; i++)
115    {
116       frswap (a,b);  
117    }
118    r2 = times(&tms2);
119    std::cout 
120         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
121         << std::endl; 
122    
123  // ----------------------------------------
124   
125    std::cout << "Use pointer function" << std::endl;
126    r1 = times(&tms1);     
127    for(i = 0 ; i< nbLoop ; i++)
128    {
129       fpswap (&a, &b);  
130    }
131    r2 = times(&tms2);
132    std::cout 
133         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
134         << std::endl;  
135    
136  // ----------------------------------------
137  
138    std::cout << "Use inline, main-defined reference function" << std::endl;
139    r1 = times(&tms1);     
140    for(i = 0 ; i< nbLoop ; i++)
141    {
142       ifrswap (a, b);  
143    }
144    r2 = times(&tms2);
145    std::cout 
146         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
147         << std::endl;    
148    
149  // ----------------------------------------
150  
151    std::cout << "Use inline, main-defined pointer function" << std::endl;
152    r1 = times(&tms1);     
153    for(i = 0 ; i< nbLoop ; i++)
154    {
155       ifpswap (&a, &b);  
156    }
157    r2 = times(&tms2);
158    std::cout 
159         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
160         << std::endl;
161
162 /*  
163 //To check the 2 following cases, just put the two 'static' functions
164 //hifpswap and  hNoifpswap in a .h
165
166    static inline void hifpswap(double *a, double *b)    
167    {
168       double tmp;
169       tmp = *a;
170       *a = *b;
171       *b = tmp;
172    }
173
174    static void hNoifpswap(double *a, double *b)    
175    {
176       double tmp;
177       tmp = *a;
178       *a = *b;
179       *b = tmp;
180    }
181     
182  // ----------------------------------------
183     
184    std::cout << "Use inline, .h defined, WITH inline keyword pointer function"
185              << std::endl;
186    r1 = times(&tms1);     
187    for(i = 0 ; i< nbLoop ; i++)
188    {
189       gdcm::Util::hifpswap (&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
199    std::cout << "Use inline, .h defined, NO inline keyword pointer function"
200              << std::endl;
201    r1 = times(&tms1);     
202    for(i = 0 ; i< nbLoop ; i++)
203    {
204       gdcm::Util::hNoifpswap (&a, &b);  
205    }
206    r2 = times(&tms2);
207    std::cout 
208         << (long) ((tms2.tms_utime)  - (tms1.tms_utime)) 
209         << std::endl; 
210 */ 
211
212 }