1 /*=========================================================================
4 Module: $RCSfile: TestInline.cxx,v $
6 Date: $Date: 2005/11/30 11:40:26 $
7 Version: $Revision: 1.11 $
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[])
170 // just to know, on every proc
171 std::cout << "Size of short int " << sizeof(short int) << std::endl;
172 std::cout << "Size of int " << sizeof(int) << std::endl;
173 std::cout << "Size of long " << sizeof(long) << std::endl;
174 std::cout << "Size of float" << sizeof(float) << std::endl;
175 std::cout << "Size of double" << sizeof(double) << std::endl;
176 std::cout << "Size of int* " << sizeof(int*) << std::endl;
177 std::cout << "-----------------" << std::endl;
182 nbLoop = atoi(argv[1]);
187 struct tms tms1, tms2;
191 uint8_t x8 =1, y8 =2;
192 uint16_t x16=1, y16=2;
193 uint32_t x32=1, y32=2;
194 float fx =1.0f, fy=1.0f;
195 double dx =1.0 , dy=1.0;
196 // ----------------------------------------
198 std::cout << "Direct "<< std::endl;
201 for(i = 0 ; i< nbLoop ; i++)
211 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
214 // ----------------------------------------
216 std::cout << "Use a macro "<< std::endl;
219 for(i = 0 ; i< nbLoop ; i++)
226 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
229 // ----------------------------------------
231 std::cout << "Use reference function" << std::endl;
234 for(i = 0 ; i< nbLoop ; i++)
241 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
244 // ----------------------------------------
246 std::cout << "Use pointer function" << std::endl;
249 for(i = 0 ; i< nbLoop ; i++)
256 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
259 // ----------------------------------------
261 std::cout << "Use inline, main-defined reference function" << std::endl;
264 for(i = 0 ; i< nbLoop ; i++)
271 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
274 // ----------------------------------------
276 std::cout << "Use inline, main-defined pointer function" << std::endl;
279 for(i = 0 ; i< nbLoop ; i++)
286 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
289 // ----------------------------------------
291 //To check the 2 following cases, we just put the two 'static' functions
292 //hifpswap and hNoifpswap in gdcmUtil.h
294 std::cout << "Use inline, .h defined, WITH inline keyword pointer method"
299 for(i = 0 ; i< nbLoop ; i++)
301 util.hifpswap (&a, &b);
306 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
310 // ----------------------------------------
312 std::cout << "Use inline, .h defined, NO inline keyword pointer method"
316 for(i = 0 ; i< nbLoop ; i++)
318 util.hNoifpswap (&a, &b);
323 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
326 // ----------------------------------------
328 std::cout << "Use , .h defined, NO inline keyword pointer method"
332 for(i = 0 ; i< nbLoop ; i++)
334 util.hfpswap (&a, &b);
339 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
342 // ----------------------------------------
344 std::cout << "Use inline, .h defined, WITH inline keyword pointer static method"
348 for(i = 0 ; i< nbLoop ; i++)
350 gdcm::Util::sthifpswap (&a, &b);
355 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
359 // ----------------------------------------
361 std::cout << "Use inline, .h defined, NO inline keyword pointer static method"
365 for(i = 0 ; i< nbLoop ; i++)
367 gdcm::Util::sthNoifpswap (&a, &b);
372 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
376 // ----------------------------------------
378 std::cout << "Pass uint_8 param directly"
382 for(i = 0 ; i< nbLoop ; i++)
384 passDirect8 (x8, y8);
389 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
392 // ----------------------------------------
394 std::cout << "Pass uint_8 param as ref"
398 for(i = 0 ; i< nbLoop ; i++)
405 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
408 // ----------------------------------------
410 std::cout << "Pass uint_8 param as ptr"
414 for(i = 0 ; i< nbLoop ; i++)
421 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
424 // ----------------------------------------
426 std::cout << "Pass uint_16 param directly"
430 for(i = 0 ; i< nbLoop ; i++)
432 passDirect16 (x16, y16);
437 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
440 // ----------------------------------------
442 std::cout << "Pass uint_16 param as ref"
446 for(i = 0 ; i< nbLoop ; i++)
448 passRef16 (x16, y16);
453 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
456 // ----------------------------------------
458 std::cout << "Pass uint_16 param as ptr"
462 for(i = 0 ; i< nbLoop ; i++)
464 passPtr16 (&x16, &y16);
469 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
474 // ----------------------------------------
476 std::cout << "Pass uint_32 param directly"
480 for(i = 0 ; i< nbLoop ; i++)
482 passDirect32 (x32, y32);
487 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
490 // ----------------------------------------
492 std::cout << "Pass uint32_t param as ref"
496 for(i = 0 ; i< nbLoop ; i++)
498 passRef32 (x32, y32 );
503 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
506 // ----------------------------------------
508 std::cout << "Pass uint_32 param as ptr"
512 for(i = 0 ; i< nbLoop ; i++)
514 passPtr32 (&x32, &y32);
519 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
522 // ----------------------------------------
524 std::cout << "Pass float param directly"
528 for(i = 0 ; i< nbLoop ; i++)
530 passDirectFloat (fx, fy);
536 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
539 // ----------------------------------------
541 std::cout << "Pass float param as ref"
545 for(i = 0 ; i< nbLoop ; i++)
547 passRefFloat (fx, fy);
553 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
556 // ----------------------------------------
558 std::cout << "Pass float param as ptr"
562 for(i = 0 ; i< nbLoop ; i++)
564 passPtrFloat (&fx, &fy);
570 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
573 // ----------------------------------------
575 std::cout << "Pass double param directly"
579 for(i = 0 ; i< nbLoop ; i++)
581 passDirectDouble (dx, dy);
587 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
590 // ----------------------------------------
592 std::cout << "Pass double param as ref"
596 for(i = 0 ; i< nbLoop ; i++)
598 passRefDouble (dx, dy);
604 << (long) ((tms2.tms_utime) - (tms1.tms_utime))
607 // ----------------------------------------
609 std::cout << "Pass double param as ptr"
613 for(i = 0 ; i< nbLoop ; i++)
615 passPtrDouble (&dx, &dy);
621 << (long) ((tms2.tms_utime) - (tms1.tms_utime))