1 /*=========================================================================
4 Module: $RCSfile: TestInline.cxx,v $
6 Date: $Date: 2007/10/29 17:13:23 $
7 Version: $Revision: 1.16 $
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.
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.
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 .cxx
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 ?!?
30 // Which CXX_FLAGS, LINKER_FLAGS, ..., must we set to see the difference?
34 #if defined(__BORLANDC__)
38 #define GET_TIME(a) a=clock()
39 #define HOW_LONG(b,a) \
40 std::cout << (double) (b-a) << std::endl
43 #define GET_TIME(a) a=clock()
44 #define HOW_LONG(b,a) \
45 std::cout << (double) (b-a) << std::endl
47 #include <sys/times.h>
48 #define GET_TIME(a) times(&a)
49 #define HOW_LONG(b,a) \
51 << (long) ((b.tms_utime) - (a.tms_utime)) \
57 void frswap (double &a, double &b);
58 void fpswap (double *a, double *b);
59 inline void ifrswap(double &a, double &b);
60 inline void ifpswap(double *a, double *b);
62 uint8_t passDirect8(uint8_t a, uint8_t b);
63 uint8_t passRef8(uint8_t &a, uint8_t &b);
64 uint8_t passPtr8(uint8_t *a, uint8_t *b);
66 uint16_t passDirect16(uint16_t a, uint16_t b);
67 uint16_t passRef16(uint16_t &a, uint16_t &b);
68 uint16_t passPtr16(uint16_t *a, uint16_t *b);
70 uint32_t passDirect32(uint32_t a, uint32_t b);
71 uint32_t passRef32(uint32_t &a, uint32_t &b);
72 uint32_t passPtr32(uint32_t *a, uint32_t *b);
74 double passDirect(double a, double b);
75 double passRef(double &a, double &b);
76 double passPtr(double *a, double *b);
86 // ============= no inline
88 void frswap(double &a, double &b)
96 void fpswap(double *a, double *b)
104 // ============= inline
106 inline void ifpswap(double *a, double *b)
114 inline void ifrswap(double &a, double &b)
123 uint32_t passRef32(uint32_t &a, uint32_t &b)
127 uint32_t passPtr32(uint32_t *a, uint32_t *b)
131 uint32_t passDirect32(uint32_t a, uint32_t b)
137 uint16_t passRef16(uint16_t &a, uint16_t &b)
141 uint16_t passPtr16(uint16_t *a, uint16_t *b)
145 uint16_t passDirect16(uint16_t a, uint16_t b)
150 uint8_t passRef8(uint8_t &a, uint8_t &b)
154 uint8_t passPtr8(uint8_t *a, uint8_t *b)
158 uint8_t passDirect8(uint8_t a, uint8_t b)
163 float passRefFloat(float &a, float &b)
167 float passPtrFloat(float *a, float *b)
171 float passDirectFloat(float a, float b)
176 double passRefDouble(double &a, double &b)
180 double passPtrDouble(double *a, double *b)
184 double passDirectDouble(double a, double b)
189 int TestInline(int argc, char *argv[])
192 #if defined(__BORLANDC__)
197 struct tms tms1, tms2;
200 // ====================================================================
202 std::cout << std::endl << std::endl
203 << "Just to be sure : sizes of native types" << std::endl
204 << "======================================="
205 << std::endl << std::endl;
206 // just to know, on every proc
207 std::cout << "Size of char " << sizeof(char) << std::endl;
208 std::cout << "Size of short int " << sizeof(short int) << std::endl;
209 std::cout << "Size of int " << sizeof(int) << std::endl;
210 std::cout << "Size of long " << sizeof(long) << std::endl;
211 std::cout << "Size of float " << sizeof(float) << std::endl;
212 std::cout << "Size of double " << sizeof(double) << std::endl;
213 std::cout << std::endl;
214 std::cout << "Size of char* " << sizeof(char*) << std::endl;
215 std::cout << "Size of short int*" << sizeof(short int*)<< std::endl;
216 std::cout << "Size of int* " << sizeof(int*) << std::endl;
217 std::cout << "Size of double* " << sizeof(double*) << std::endl;
218 std::cout << "-----------------" << std::endl;
220 // ====================================================================
226 nbLoop = atoi(argv[1]);
230 uint8_t x8 =1, y8 =2;
231 uint16_t x16=1, y16=2;
232 uint32_t x32=1, y32=2;
233 float fx =1.0f, fy=1.0f;
234 double dx =1.0 , dy=1.0;
237 // ====================================================================
239 std::cout << std::endl << std::endl
240 << "Check different ways of passing scalars to a function "<< nbLoop << " times" << std::endl
241 << "====================================================="
242 << std::endl << std::endl;
244 std::cout << "Pass uint_8 param directly"
248 for(i = 0 ; i< nbLoop ; i++)
250 passDirect8 (x8, y8);
255 // ----------------------------------------
257 std::cout << "Pass uint_8 param as ref"
261 for(i = 0 ; i< nbLoop ; i++)
268 // ----------------------------------------
270 std::cout << "Pass uint_8 param as ptr"
274 for(i = 0 ; i< nbLoop ; i++)
281 // ----------------------------------------
282 std::cout << std::endl;
283 std::cout << "Pass uint_16 param directly"
287 for(i = 0 ; i< nbLoop ; i++)
289 passDirect16 (x16, y16);
294 // ----------------------------------------
296 std::cout << "Pass uint_16 param as ref"
300 for(i = 0 ; i< nbLoop ; i++)
302 passRef16 (x16, y16);
307 // ----------------------------------------
309 std::cout << "Pass uint_16 param as ptr"
313 for(i = 0 ; i< nbLoop ; i++)
315 passPtr16 (&x16, &y16);
320 // ----------------------------------------
321 std::cout << std::endl;
322 std::cout << "Pass uint_32 param directly"
326 for(i = 0 ; i< nbLoop ; i++)
328 passDirect32 (x32, y32);
333 // ----------------------------------------
335 std::cout << "Pass uint32_t param as ref"
339 for(i = 0 ; i< nbLoop ; i++)
341 passRef32 (x32, y32 );
346 // ----------------------------------------
348 std::cout << "Pass uint_32 param as ptr"
352 for(i = 0 ; i< nbLoop ; i++)
354 passPtr32 (&x32, &y32);
359 // ----------------------------------------
360 std::cout << std::endl;
361 std::cout << "Pass float param directly"
365 for(i = 0 ; i< nbLoop ; i++)
367 passDirectFloat (fx, fy);
372 // ----------------------------------------
374 std::cout << "Pass float param as ref"
378 for(i = 0 ; i< nbLoop ; i++)
380 passRefFloat (fx, fy);
385 // ----------------------------------------
387 std::cout << "Pass float param as ptr"
391 for(i = 0 ; i< nbLoop ; i++)
393 passPtrFloat (&fx, &fy);
398 // ----------------------------------------
399 std::cout << std::endl;
400 std::cout << "Pass double param directly"
404 for(i = 0 ; i< nbLoop ; i++)
406 passDirectDouble (dx, dy);
411 // ----------------------------------------
413 std::cout << "Pass double param as ref"
417 for(i = 0 ; i< nbLoop ; i++)
419 passRefDouble (dx, dy);
423 // ----------------------------------------
425 std::cout << "Pass double param as ptr"
429 for(i = 0 ; i< nbLoop ; i++)
431 passPtrDouble (&dx, &dy);
437 // ====================================================================
439 std::cout << std::endl;
440 std::cout << "Exchange 2 scalars " << nbLoop << " times" << std::endl
441 << "==================="
442 << std::endl << std::endl;
444 // ----------------------------------------
446 std::cout << "Direct "<< std::endl;
449 for(i = 0 ; i< nbLoop ; i++)
460 // ----------------------------------------
462 std::cout << "Use a macro "<< std::endl;
465 for(i = 0 ; i< nbLoop ; i++)
472 // ----------------------------------------
474 std::cout << std::endl;
475 std::cout << "Use a function, param passed by reference" << std::endl;
478 for(i = 0 ; i< nbLoop ; i++)
485 // ----------------------------------------
487 std::cout << "Use a function, param passed by pointer" << std::endl;
490 for(i = 0 ; i< nbLoop ; i++)
497 // ----------------------------------------
499 std::cout << std::endl;
501 std::cout << "Use inline, .cxx-defined function, param passed by reference" << std::endl;
504 for(i = 0 ; i< nbLoop ; i++)
511 // ----------------------------------------
513 std::cout << "Use inline, .cxx-defined function, param passed by pointer" << std::endl;
516 for(i = 0 ; i< nbLoop ; i++)
523 // ----------------------------------------
525 std::cout << std::endl;
527 //To check the 2 following cases, we just put the two 'static' functions
528 //hifpswap and hNoifpswap in gdcmUtil.h
530 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer"
536 for(i = 0 ; i< nbLoop ; i++)
538 util.hifpswap (&a, &b);
544 // ----------------------------------------
546 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer"
550 for(i = 0 ; i< nbLoop ; i++)
552 util.hNoifpswap (&a, &b);
557 // ----------------------------------------
558 std::cout << std::endl;
560 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer STATIC function"
564 for(i = 0 ; i< nbLoop ; i++)
566 gdcm::Util::sthifpswap (&a, &b);
571 // ----------------------------------------
573 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer STATIC function"
577 for(i = 0 ; i< nbLoop ; i++)
579 gdcm::Util::sthNoifpswap (&a, &b);
584 //return 1; // will generate an error,
585 // just to allow us to see the full log in the dashboard