1 /*=========================================================================
4 Module: $RCSfile: TestInline.cxx,v $
6 Date: $Date: 2007/09/20 12:48:21 $
7 Version: $Revision: 1.12 $
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 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 ?!?
30 // Which CXX_FLAGS, LINKER_FLAGS, ..., must we set to see the difference?
34 #include <sys/times.h>
37 void frswap (double &a, double &b);
38 void fpswap (double *a, double *b);
39 inline void ifrswap(double &a, double &b);
40 inline void ifpswap(double *a, double *b);
42 uint8_t passDirect8(uint8_t a, uint8_t b);
43 uint8_t passRef8(uint8_t &a, uint8_t &b);
44 uint8_t passPtr8(uint8_t *a, uint8_t *b);
46 uint16_t passDirect16(uint16_t a, uint16_t b);
47 uint16_t passRef16(uint16_t &a, uint16_t &b);
48 uint16_t passPtr16(uint16_t *a, uint16_t *b);
50 uint32_t passDirect32(uint32_t a, uint32_t b);
51 uint32_t passRef32(uint32_t &a, uint32_t &b);
52 uint32_t passPtr32(uint32_t *a, uint32_t *b);
54 double passDirect(double a, double b);
55 double passRef(double &a, double &b);
56 double passPtr(double *a, double *b);
66 void frswap(double &a, double &b)
75 void fpswap(double *a, double *b)
84 inline void ifpswap(double *a, double *b)
92 inline void ifrswap(double &a, double &b)
101 uint32_t passRef32(uint32_t &a, uint32_t &b)
105 uint32_t passPtr32(uint32_t *a, uint32_t *b)
109 uint32_t passDirect32(uint32_t a, uint32_t b)
115 uint16_t passRef16(uint16_t &a, uint16_t &b)
119 uint16_t passPtr16(uint16_t *a, uint16_t *b)
123 uint16_t passDirect16(uint16_t a, uint16_t b)
128 uint8_t passRef8(uint8_t &a, uint8_t &b)
132 uint8_t passPtr8(uint8_t *a, uint8_t *b)
136 uint8_t passDirect8(uint8_t a, uint8_t b)
141 float passRefFloat(float &a, float &b)
145 float passPtrFloat(float *a, float *b)
149 float passDirectFloat(float a, float b)
154 double passRefDouble(double &a, double &b)
158 double passPtrDouble(double *a, double *b)
162 double passDirectDouble(double a, double b)
167 int TestInline(int argc, char *argv[])
169 std::cout << std::endl << std::endl
170 << "Just to be sure : sizes of native types"
171 << std::endl << std::endl;
172 // just to know, on every proc
173 std::cout << "Size of short int " << sizeof(short int) << std::endl;
174 std::cout << "Size of int " << sizeof(int) << std::endl;
175 std::cout << "Size of long " << sizeof(long) << std::endl;
176 std::cout << "Size of float" << sizeof(float) << std::endl;
177 std::cout << "Size of double" << sizeof(double) << std::endl;
178 std::cout << "Size of int* " << sizeof(int*) << std::endl;
179 std::cout << "-----------------" << std::endl;
184 nbLoop = atoi(argv[1]);
189 struct tms tms1, tms2;
193 std::cout << "Exchange 2 scalars " << nbLoop << " times"
194 << std::endl << std::endl;
195 uint8_t x8 =1, y8 =2;
196 uint16_t x16=1, y16=2;
197 uint32_t x32=1, y32=2;
198 float fx =1.0f, fy=1.0f;
199 double dx =1.0 , dy=1.0;
200 // ----------------------------------------
202 std::cout << "Direct "<< std::endl;
205 for(i = 0 ; i< nbLoop ; i++)
215 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
218 // ----------------------------------------
220 std::cout << "Use a macro "<< std::endl;
223 for(i = 0 ; i< nbLoop ; i++)
230 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
233 // ----------------------------------------
235 std::cout << "Use reference function" << std::endl;
238 for(i = 0 ; i< nbLoop ; i++)
245 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
248 // ----------------------------------------
250 std::cout << "Use pointer function" << std::endl;
253 for(i = 0 ; i< nbLoop ; i++)
260 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
263 // ----------------------------------------
265 std::cout << "Use inline, main-defined reference function" << std::endl;
268 for(i = 0 ; i< nbLoop ; i++)
275 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
278 // ----------------------------------------
280 std::cout << "Use inline, main-defined pointer function" << std::endl;
283 for(i = 0 ; i< nbLoop ; i++)
290 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
293 // ----------------------------------------
295 //To check the 2 following cases, we just put the two 'static' functions
296 //hifpswap and hNoifpswap in gdcmUtil.h
298 std::cout << "Use inline, .h defined, WITH inline keyword pointer method"
303 for(i = 0 ; i< nbLoop ; i++)
305 util.hifpswap (&a, &b);
310 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
314 // ----------------------------------------
316 std::cout << "Use inline, .h defined, NO inline keyword pointer method"
320 for(i = 0 ; i< nbLoop ; i++)
322 util.hNoifpswap (&a, &b);
327 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
330 // ----------------------------------------
332 std::cout << "Use , .h defined, NO inline keyword pointer method"
336 for(i = 0 ; i< nbLoop ; i++)
338 util.hfpswap (&a, &b);
343 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
346 // ----------------------------------------
348 std::cout << "Use inline, .h defined, WITH inline keyword pointer static method"
352 for(i = 0 ; i< nbLoop ; i++)
354 gdcm::Util::sthifpswap (&a, &b);
359 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
363 // ----------------------------------------
365 std::cout << "Use inline, .h defined, NO inline keyword pointer static method"
369 for(i = 0 ; i< nbLoop ; i++)
371 gdcm::Util::sthNoifpswap (&a, &b);
376 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
380 // ----------------------------------------
381 std::cout << std::endl << std::endl
382 << "Check different ways of passing scalars to a function, "
383 << nbLoop << " times" << std::endl << std::endl;
385 std::cout << "Pass uint_8 param directly"
389 for(i = 0 ; i< nbLoop ; i++)
391 passDirect8 (x8, y8);
396 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
399 // ----------------------------------------
401 std::cout << "Pass uint_8 param as ref"
405 for(i = 0 ; i< nbLoop ; i++)
412 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
415 // ----------------------------------------
417 std::cout << "Pass uint_8 param as ptr"
421 for(i = 0 ; i< nbLoop ; i++)
428 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
431 // ----------------------------------------
432 std::cout << std::endl;
433 std::cout << "Pass uint_16 param directly"
437 for(i = 0 ; i< nbLoop ; i++)
439 passDirect16 (x16, y16);
444 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
447 // ----------------------------------------
449 std::cout << "Pass uint_16 param as ref"
453 for(i = 0 ; i< nbLoop ; i++)
455 passRef16 (x16, y16);
460 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
463 // ----------------------------------------
465 std::cout << "Pass uint_16 param as ptr"
469 for(i = 0 ; i< nbLoop ; i++)
471 passPtr16 (&x16, &y16);
476 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
481 // ----------------------------------------
482 std::cout << std::endl;
483 std::cout << "Pass uint_32 param directly"
487 for(i = 0 ; i< nbLoop ; i++)
489 passDirect32 (x32, y32);
494 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
497 // ----------------------------------------
499 std::cout << "Pass uint32_t param as ref"
503 for(i = 0 ; i< nbLoop ; i++)
505 passRef32 (x32, y32 );
510 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
513 // ----------------------------------------
515 std::cout << "Pass uint_32 param as ptr"
519 for(i = 0 ; i< nbLoop ; i++)
521 passPtr32 (&x32, &y32);
526 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
529 // ----------------------------------------
530 std::cout << std::endl;
531 std::cout << "Pass float param directly"
535 for(i = 0 ; i< nbLoop ; i++)
537 passDirectFloat (fx, fy);
543 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
546 // ----------------------------------------
548 std::cout << "Pass float param as ref"
552 for(i = 0 ; i< nbLoop ; i++)
554 passRefFloat (fx, fy);
560 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
563 // ----------------------------------------
565 std::cout << "Pass float param as ptr"
569 for(i = 0 ; i< nbLoop ; i++)
571 passPtrFloat (&fx, &fy);
577 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
580 // ----------------------------------------
581 std::cout << std::endl;
582 std::cout << "Pass double param directly"
586 for(i = 0 ; i< nbLoop ; i++)
588 passDirectDouble (dx, dy);
594 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
597 // ----------------------------------------
599 std::cout << "Pass double param as ref"
603 for(i = 0 ; i< nbLoop ; i++)
605 passRefDouble (dx, dy);
611 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
614 // ----------------------------------------
616 std::cout << "Pass double param as ptr"
620 for(i = 0 ; i< nbLoop ; i++)
622 passPtrDouble (&dx, &dy);
628 << (long) ((tms2.tms_utime) - (tms1.tms_utime))