1 /*=========================================================================
4 Module: $RCSfile: TestInline.cxx,v $
6 Date: $Date: 2007/10/30 09:07:46 $
7 Version: $Revision: 1.17 $
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__) || defined (_MSC_VER)
36 #if defined(__BORLANDC__)
40 #define GET_TIME(a) a=clock()
41 #define HOW_LONG(b,a) \
42 std::cout << (double) (b-a) << std::endl
45 #include <sys/times.h>
46 #define GET_TIME(a) times(&a)
47 #define HOW_LONG(b,a) \
49 << (long) ((b.tms_utime) - (a.tms_utime)) \
55 void frswap (double &a, double &b);
56 void fpswap (double *a, double *b);
57 inline void ifrswap(double &a, double &b);
58 inline void ifpswap(double *a, double *b);
60 uint8_t passDirect8(uint8_t a, uint8_t b);
61 uint8_t passRef8(uint8_t &a, uint8_t &b);
62 uint8_t passPtr8(uint8_t *a, uint8_t *b);
64 uint16_t passDirect16(uint16_t a, uint16_t b);
65 uint16_t passRef16(uint16_t &a, uint16_t &b);
66 uint16_t passPtr16(uint16_t *a, uint16_t *b);
68 uint32_t passDirect32(uint32_t a, uint32_t b);
69 uint32_t passRef32(uint32_t &a, uint32_t &b);
70 uint32_t passPtr32(uint32_t *a, uint32_t *b);
72 double passDirect(double a, double b);
73 double passRef(double &a, double &b);
74 double passPtr(double *a, double *b);
84 // ============= no inline
86 void frswap(double &a, double &b)
94 void fpswap(double *a, double *b)
102 // ============= inline
104 inline void ifpswap(double *a, double *b)
112 inline void ifrswap(double &a, double &b)
121 uint32_t passRef32(uint32_t &a, uint32_t &b)
125 uint32_t passPtr32(uint32_t *a, uint32_t *b)
129 uint32_t passDirect32(uint32_t a, uint32_t b)
135 uint16_t passRef16(uint16_t &a, uint16_t &b)
139 uint16_t passPtr16(uint16_t *a, uint16_t *b)
143 uint16_t passDirect16(uint16_t a, uint16_t b)
148 uint8_t passRef8(uint8_t &a, uint8_t &b)
152 uint8_t passPtr8(uint8_t *a, uint8_t *b)
156 uint8_t passDirect8(uint8_t a, uint8_t b)
161 float passRefFloat(float &a, float &b)
165 float passPtrFloat(float *a, float *b)
169 float passDirectFloat(float a, float b)
174 double passRefDouble(double &a, double &b)
178 double passPtrDouble(double *a, double *b)
182 double passDirectDouble(double a, double b)
187 int TestInline(int argc, char *argv[])
190 #if defined(__BORLANDC__) || defined (_MSC_VER)
193 struct tms tms1, tms2;
196 // ====================================================================
198 std::cout << std::endl << std::endl
199 << "Just to be sure : sizes of native types" << std::endl
200 << "======================================="
201 << std::endl << std::endl;
202 // just to know, on every proc
203 std::cout << "Size of char " << sizeof(char) << std::endl;
204 std::cout << "Size of short int " << sizeof(short int) << std::endl;
205 std::cout << "Size of int " << sizeof(int) << std::endl;
206 std::cout << "Size of long " << sizeof(long) << std::endl;
207 std::cout << "Size of float " << sizeof(float) << std::endl;
208 std::cout << "Size of double " << sizeof(double) << std::endl;
209 std::cout << std::endl;
210 std::cout << "Size of char* " << sizeof(char*) << std::endl;
211 std::cout << "Size of short int*" << sizeof(short int*)<< std::endl;
212 std::cout << "Size of int* " << sizeof(int*) << std::endl;
213 std::cout << "Size of double* " << sizeof(double*) << std::endl;
214 std::cout << "-----------------" << std::endl;
216 // ====================================================================
222 nbLoop = atoi(argv[1]);
226 uint8_t x8 =1, y8 =2;
227 uint16_t x16=1, y16=2;
228 uint32_t x32=1, y32=2;
229 float fx =1.0f, fy=1.0f;
230 double dx =1.0 , dy=1.0;
233 // ====================================================================
235 std::cout << std::endl << std::endl
236 << "Check different ways of passing scalars to a function "<< nbLoop << " times" << std::endl
237 << "====================================================="
238 << std::endl << std::endl;
240 std::cout << "Pass uint_8 param directly"
244 for(i = 0 ; i< nbLoop ; i++)
246 passDirect8 (x8, y8);
251 // ----------------------------------------
253 std::cout << "Pass uint_8 param as ref"
257 for(i = 0 ; i< nbLoop ; i++)
264 // ----------------------------------------
266 std::cout << "Pass uint_8 param as ptr"
270 for(i = 0 ; i< nbLoop ; i++)
277 // ----------------------------------------
278 std::cout << std::endl;
279 std::cout << "Pass uint_16 param directly"
283 for(i = 0 ; i< nbLoop ; i++)
285 passDirect16 (x16, y16);
290 // ----------------------------------------
292 std::cout << "Pass uint_16 param as ref"
296 for(i = 0 ; i< nbLoop ; i++)
298 passRef16 (x16, y16);
303 // ----------------------------------------
305 std::cout << "Pass uint_16 param as ptr"
309 for(i = 0 ; i< nbLoop ; i++)
311 passPtr16 (&x16, &y16);
316 // ----------------------------------------
317 std::cout << std::endl;
318 std::cout << "Pass uint_32 param directly"
322 for(i = 0 ; i< nbLoop ; i++)
324 passDirect32 (x32, y32);
329 // ----------------------------------------
331 std::cout << "Pass uint32_t param as ref"
335 for(i = 0 ; i< nbLoop ; i++)
337 passRef32 (x32, y32 );
342 // ----------------------------------------
344 std::cout << "Pass uint_32 param as ptr"
348 for(i = 0 ; i< nbLoop ; i++)
350 passPtr32 (&x32, &y32);
355 // ----------------------------------------
356 std::cout << std::endl;
357 std::cout << "Pass float param directly"
361 for(i = 0 ; i< nbLoop ; i++)
363 passDirectFloat (fx, fy);
368 // ----------------------------------------
370 std::cout << "Pass float param as ref"
374 for(i = 0 ; i< nbLoop ; i++)
376 passRefFloat (fx, fy);
381 // ----------------------------------------
383 std::cout << "Pass float param as ptr"
387 for(i = 0 ; i< nbLoop ; i++)
389 passPtrFloat (&fx, &fy);
394 // ----------------------------------------
395 std::cout << std::endl;
396 std::cout << "Pass double param directly"
400 for(i = 0 ; i< nbLoop ; i++)
402 passDirectDouble (dx, dy);
407 // ----------------------------------------
409 std::cout << "Pass double param as ref"
413 for(i = 0 ; i< nbLoop ; i++)
415 passRefDouble (dx, dy);
419 // ----------------------------------------
421 std::cout << "Pass double param as ptr"
425 for(i = 0 ; i< nbLoop ; i++)
427 passPtrDouble (&dx, &dy);
433 // ====================================================================
435 std::cout << std::endl;
436 std::cout << "Exchange 2 scalars " << nbLoop << " times" << std::endl
437 << "==================="
438 << std::endl << std::endl;
440 // ----------------------------------------
442 std::cout << "Direct "<< std::endl;
445 for(i = 0 ; i< nbLoop ; i++)
456 // ----------------------------------------
458 std::cout << "Use a macro "<< std::endl;
461 for(i = 0 ; i< nbLoop ; i++)
468 // ----------------------------------------
470 std::cout << std::endl;
471 std::cout << "Use a function, param passed by reference" << std::endl;
474 for(i = 0 ; i< nbLoop ; i++)
481 // ----------------------------------------
483 std::cout << "Use a function, param passed by pointer" << std::endl;
486 for(i = 0 ; i< nbLoop ; i++)
493 // ----------------------------------------
495 std::cout << std::endl;
497 std::cout << "Use inline, .cxx-defined function, param passed by reference" << std::endl;
500 for(i = 0 ; i< nbLoop ; i++)
507 // ----------------------------------------
509 std::cout << "Use inline, .cxx-defined function, param passed by pointer" << std::endl;
512 for(i = 0 ; i< nbLoop ; i++)
519 // ----------------------------------------
521 std::cout << std::endl;
523 //To check the 2 following cases, we just put the two 'static' functions
524 //hifpswap and hNoifpswap in gdcmUtil.h
526 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer"
532 for(i = 0 ; i< nbLoop ; i++)
534 util.hifpswap (&a, &b);
540 // ----------------------------------------
542 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer"
546 for(i = 0 ; i< nbLoop ; i++)
548 util.hNoifpswap (&a, &b);
553 // ----------------------------------------
554 std::cout << std::endl;
556 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer STATIC function"
560 for(i = 0 ; i< nbLoop ; i++)
562 gdcm::Util::sthifpswap (&a, &b);
567 // ----------------------------------------
569 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer STATIC function"
573 for(i = 0 ; i< nbLoop ; i++)
575 gdcm::Util::sthNoifpswap (&a, &b);
580 //return 1; // will generate an error,
581 // just to allow us to see the full log in the dashboard