1 /*=========================================================================
4 Module: $RCSfile: TestInline.cxx,v $
6 Date: $Date: 2008/09/19 09:33:17 $
7 Version: $Revision: 1.21 $
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?
33 #include <string.h> // Needed under Suse?!?
34 #include <stdlib.h> // atoi
35 #if defined(__BORLANDC__) || defined (_MSC_VER)
37 #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 << "Size of bool " << sizeof(bool) << std::endl;
210 std::cout << std::endl;
211 std::cout << "Size of char* " << sizeof(char*) << std::endl;
212 std::cout << "Size of short int*" << sizeof(short int*)<< std::endl;
213 std::cout << "Size of int* " << sizeof(int*) << std::endl;
214 std::cout << "Size of double* " << sizeof(double*) << std::endl;
215 std::cout << "-----------------" << std::endl;
217 // ====================================================================
223 nbLoop = atoi(argv[1]);
227 uint8_t x8 =1, y8 =2;
228 uint16_t x16=1, y16=2;
229 uint32_t x32=1, y32=2;
230 float fx =1.0f, fy=1.0f;
231 double dx =1.0 , dy=1.0;
234 // ====================================================================
236 std::cout << std::endl << std::endl
237 << "Check different ways of passing scalars to a function "<< nbLoop << " times" << std::endl
238 << "====================================================="
239 << std::endl << std::endl;
241 std::cout << "Pass uint_8 param directly"
245 for(i = 0 ; i< nbLoop ; i++)
247 passDirect8 (x8, y8);
252 // ----------------------------------------
254 std::cout << "Pass uint_8 param as ref"
258 for(i = 0 ; i< nbLoop ; i++)
265 // ----------------------------------------
267 std::cout << "Pass uint_8 param as ptr"
271 for(i = 0 ; i< nbLoop ; i++)
278 // ----------------------------------------
279 std::cout << std::endl;
280 std::cout << "Pass uint_16 param directly"
284 for(i = 0 ; i< nbLoop ; i++)
286 passDirect16 (x16, y16);
291 // ----------------------------------------
293 std::cout << "Pass uint_16 param as ref"
297 for(i = 0 ; i< nbLoop ; i++)
299 passRef16 (x16, y16);
304 // ----------------------------------------
306 std::cout << "Pass uint_16 param as ptr"
310 for(i = 0 ; i< nbLoop ; i++)
312 passPtr16 (&x16, &y16);
317 // ----------------------------------------
318 std::cout << std::endl;
319 std::cout << "Pass uint_32 param directly"
323 for(i = 0 ; i< nbLoop ; i++)
325 passDirect32 (x32, y32);
330 // ----------------------------------------
332 std::cout << "Pass uint32_t param as ref"
336 for(i = 0 ; i< nbLoop ; i++)
338 passRef32 (x32, y32 );
343 // ----------------------------------------
345 std::cout << "Pass uint_32 param as ptr"
349 for(i = 0 ; i< nbLoop ; i++)
351 passPtr32 (&x32, &y32);
356 // ----------------------------------------
357 std::cout << std::endl;
358 std::cout << "Pass float param directly"
362 for(i = 0 ; i< nbLoop ; i++)
364 passDirectFloat (fx, fy);
369 // ----------------------------------------
371 std::cout << "Pass float param as ref"
375 for(i = 0 ; i< nbLoop ; i++)
377 passRefFloat (fx, fy);
382 // ----------------------------------------
384 std::cout << "Pass float param as ptr"
388 for(i = 0 ; i< nbLoop ; i++)
390 passPtrFloat (&fx, &fy);
395 // ----------------------------------------
396 std::cout << std::endl;
397 std::cout << "Pass double param directly"
401 for(i = 0 ; i< nbLoop ; i++)
403 passDirectDouble (dx, dy);
408 // ----------------------------------------
410 std::cout << "Pass double param as ref"
414 for(i = 0 ; i< nbLoop ; i++)
416 passRefDouble (dx, dy);
420 // ----------------------------------------
422 std::cout << "Pass double param as ptr"
426 for(i = 0 ; i< nbLoop ; i++)
428 passPtrDouble (&dx, &dy);
434 // ====================================================================
436 std::cout << std::endl;
437 std::cout << "Exchange 2 scalars " << nbLoop << " times" << std::endl
438 << "==================="
439 << std::endl << std::endl;
441 // ----------------------------------------
443 std::cout << "Direct "<< std::endl;
446 for(i = 0 ; i< nbLoop ; i++)
457 // ----------------------------------------
459 std::cout << "Use a macro "<< std::endl;
462 for(i = 0 ; i< nbLoop ; i++)
469 // ----------------------------------------
471 std::cout << std::endl;
472 std::cout << "Use a function, param passed by reference" << std::endl;
475 for(i = 0 ; i< nbLoop ; i++)
482 // ----------------------------------------
484 std::cout << "Use a function, param passed by pointer" << std::endl;
487 for(i = 0 ; i< nbLoop ; i++)
494 // ----------------------------------------
496 std::cout << std::endl;
498 std::cout << "Use inline, .cxx-defined function, param passed by reference" << std::endl;
501 for(i = 0 ; i< nbLoop ; i++)
508 // ----------------------------------------
510 std::cout << "Use inline, .cxx-defined function, param passed by pointer" << std::endl;
513 for(i = 0 ; i< nbLoop ; i++)
520 // ----------------------------------------
522 std::cout << std::endl;
524 //To check the 2 following cases, we just put the two 'static' functions
525 //hifpswap and hNoifpswap in gdcmUtil.h
527 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer"
530 GDCM_NAME_SPACE::Util util;
533 for(i = 0 ; i< nbLoop ; i++)
535 util.hifpswap (&a, &b);
541 // ----------------------------------------
543 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer"
547 for(i = 0 ; i< nbLoop ; i++)
549 util.hNoifpswap (&a, &b);
554 // ----------------------------------------
555 std::cout << std::endl;
557 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer STATIC function"
561 for(i = 0 ; i< nbLoop ; i++)
563 GDCM_NAME_SPACE::Util::sthifpswap (&a, &b);
568 // ----------------------------------------
570 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer STATIC function"
574 for(i = 0 ; i< nbLoop ; i++)
576 GDCM_NAME_SPACE::Util::sthNoifpswap (&a, &b);
581 // ----------------------------------------
583 // Just to point out that playing with pointers doesn't save so much time ...
585 std::cout << "Play with arrays\n================" << std::endl;
589 std::cout << "Copy 2 arrays [i][j]"
593 unsigned short int z1[128][3118], z2[128][3118];
595 for(i = 0 ; i< nbLoop ; i++)
597 unsigned short int *pv1=&z1[0][0], *pv2=&z2[0][0];
598 for (int j=0;j<3118;j++)
599 for(int i=0; i<128;i++)
605 std::cout << "Copy 2 arrays ([i][j], pointer)"
609 for(i = 0 ; i< nbLoop ; i++)
611 unsigned short int *pv1=&z1[0][0], *pv2=&z2[0][0];
612 for (int j=0;j<3118;j++)
613 for(int i=0; i<128;i++)
619 std::cout << "Copy 2 arrays (2 pointers)" << std::endl;
621 // unsigned short int w1[3118*128], w2[3118][128];
622 for(i = 0 ; i< nbLoop ; i++)
624 unsigned short int *pw1=&z1[0][0], *pw2=&z2[0][0];
625 for (int j=0;j<3118;j++)
626 for(int i=0; i<128;i++)
633 std::cout << "Copy 2 arrays (memcpy)" << std::endl;
635 for(i = 0 ; i< nbLoop ; i++)
637 unsigned short int *pw1=&z1[0][0], *pw2=&z2[0][0];
638 memcpy(pw2,pw1,3118*128*sizeof(unsigned short int));
645 std::cout << "Transpose 2 arrays [i][j]"
648 unsigned short int t1[3118][128], t2[128][3118];
650 for(i = 0 ; i< nbLoop ; i++)
652 unsigned short int *pv1=&t1[0][0], *pv2=&t2[0][0];
653 for (int j=0;j<3118;j++)
654 for(int i=0; i<128;i++)
661 std::cout << "Transpose 2 arrays ([i][j], pointer)"
664 unsigned short int w1[3118*128], w2[3118][128];
666 for(i = 0 ; i< nbLoop ; i++)
668 unsigned short int *pw1=w1, *pw2=&w2[0][0];
669 for (int j=0;j<3118;j++)
670 for(int i=0; i<128;i++)
677 std::cout << "Transpose 2 arrays (2 pointers)"
680 unsigned short int v1[3118*128], v2[128*3118];
682 for(i = 0 ; i< nbLoop ; i++)
684 unsigned short int *pv1=v1, *pv2=v2;
685 for (int j=0;j<3118;j++)
686 for(int i=0; i<128;i++)
687 *(pv2+i*128+j) = *pv1++;
694 //return 1; // will generate an error,
695 // just to allow us to see the full log in the dashboard