1 /*=========================================================================
4 Module: $RCSfile: TestInline.cxx,v $
6 Date: $Date: 2007/09/28 07:35:49 $
7 Version: $Revision: 1.15 $
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?
36 #define GET_TIME(a) a=clock()
37 #define HOW_LONG(b,a) \
38 std::cout << (double) (b-a) << std::endl
40 #include <sys/times.h>
41 #define GET_TIME(a) times(&a)
42 #define HOW_LONG(b,a) \
44 << (long) ((b.tms_utime) - (a.tms_utime)) \
50 void frswap (double &a, double &b);
51 void fpswap (double *a, double *b);
52 inline void ifrswap(double &a, double &b);
53 inline void ifpswap(double *a, double *b);
55 uint8_t passDirect8(uint8_t a, uint8_t b);
56 uint8_t passRef8(uint8_t &a, uint8_t &b);
57 uint8_t passPtr8(uint8_t *a, uint8_t *b);
59 uint16_t passDirect16(uint16_t a, uint16_t b);
60 uint16_t passRef16(uint16_t &a, uint16_t &b);
61 uint16_t passPtr16(uint16_t *a, uint16_t *b);
63 uint32_t passDirect32(uint32_t a, uint32_t b);
64 uint32_t passRef32(uint32_t &a, uint32_t &b);
65 uint32_t passPtr32(uint32_t *a, uint32_t *b);
67 double passDirect(double a, double b);
68 double passRef(double &a, double &b);
69 double passPtr(double *a, double *b);
79 // ============= no inline
81 void frswap(double &a, double &b)
89 void fpswap(double *a, double *b)
97 // ============= inline
99 inline void ifpswap(double *a, double *b)
107 inline void ifrswap(double &a, double &b)
116 uint32_t passRef32(uint32_t &a, uint32_t &b)
120 uint32_t passPtr32(uint32_t *a, uint32_t *b)
124 uint32_t passDirect32(uint32_t a, uint32_t b)
130 uint16_t passRef16(uint16_t &a, uint16_t &b)
134 uint16_t passPtr16(uint16_t *a, uint16_t *b)
138 uint16_t passDirect16(uint16_t a, uint16_t b)
143 uint8_t passRef8(uint8_t &a, uint8_t &b)
147 uint8_t passPtr8(uint8_t *a, uint8_t *b)
151 uint8_t passDirect8(uint8_t a, uint8_t b)
156 float passRefFloat(float &a, float &b)
160 float passPtrFloat(float *a, float *b)
164 float passDirectFloat(float a, float b)
169 double passRefDouble(double &a, double &b)
173 double passPtrDouble(double *a, double *b)
177 double passDirectDouble(double a, double b)
182 int TestInline(int argc, char *argv[])
188 struct tms tms1, tms2;
191 // ====================================================================
193 std::cout << std::endl << std::endl
194 << "Just to be sure : sizes of native types" << std::endl
195 << "======================================="
196 << std::endl << std::endl;
197 // just to know, on every proc
198 std::cout << "Size of char " << sizeof(char) << std::endl;
199 std::cout << "Size of short int " << sizeof(short int) << std::endl;
200 std::cout << "Size of int " << sizeof(int) << std::endl;
201 std::cout << "Size of long " << sizeof(long) << std::endl;
202 std::cout << "Size of float " << sizeof(float) << std::endl;
203 std::cout << "Size of double " << sizeof(double) << std::endl;
204 std::cout << std::endl;
205 std::cout << "Size of char* " << sizeof(char*) << std::endl;
206 std::cout << "Size of short int*" << sizeof(short int*)<< std::endl;
207 std::cout << "Size of int* " << sizeof(int*) << std::endl;
208 std::cout << "Size of double* " << sizeof(double*) << std::endl;
209 std::cout << "-----------------" << std::endl;
211 // ====================================================================
217 nbLoop = atoi(argv[1]);
221 uint8_t x8 =1, y8 =2;
222 uint16_t x16=1, y16=2;
223 uint32_t x32=1, y32=2;
224 float fx =1.0f, fy=1.0f;
225 double dx =1.0 , dy=1.0;
228 // ====================================================================
230 std::cout << std::endl << std::endl
231 << "Check different ways of passing scalars to a function "<< nbLoop << " times" << std::endl
232 << "====================================================="
233 << std::endl << std::endl;
235 std::cout << "Pass uint_8 param directly"
239 for(i = 0 ; i< nbLoop ; i++)
241 passDirect8 (x8, y8);
246 // ----------------------------------------
248 std::cout << "Pass uint_8 param as ref"
252 for(i = 0 ; i< nbLoop ; i++)
259 // ----------------------------------------
261 std::cout << "Pass uint_8 param as ptr"
265 for(i = 0 ; i< nbLoop ; i++)
272 // ----------------------------------------
273 std::cout << std::endl;
274 std::cout << "Pass uint_16 param directly"
278 for(i = 0 ; i< nbLoop ; i++)
280 passDirect16 (x16, y16);
285 // ----------------------------------------
287 std::cout << "Pass uint_16 param as ref"
291 for(i = 0 ; i< nbLoop ; i++)
293 passRef16 (x16, y16);
298 // ----------------------------------------
300 std::cout << "Pass uint_16 param as ptr"
304 for(i = 0 ; i< nbLoop ; i++)
306 passPtr16 (&x16, &y16);
311 // ----------------------------------------
312 std::cout << std::endl;
313 std::cout << "Pass uint_32 param directly"
317 for(i = 0 ; i< nbLoop ; i++)
319 passDirect32 (x32, y32);
324 // ----------------------------------------
326 std::cout << "Pass uint32_t param as ref"
330 for(i = 0 ; i< nbLoop ; i++)
332 passRef32 (x32, y32 );
337 // ----------------------------------------
339 std::cout << "Pass uint_32 param as ptr"
343 for(i = 0 ; i< nbLoop ; i++)
345 passPtr32 (&x32, &y32);
350 // ----------------------------------------
351 std::cout << std::endl;
352 std::cout << "Pass float param directly"
356 for(i = 0 ; i< nbLoop ; i++)
358 passDirectFloat (fx, fy);
363 // ----------------------------------------
365 std::cout << "Pass float param as ref"
369 for(i = 0 ; i< nbLoop ; i++)
371 passRefFloat (fx, fy);
376 // ----------------------------------------
378 std::cout << "Pass float param as ptr"
382 for(i = 0 ; i< nbLoop ; i++)
384 passPtrFloat (&fx, &fy);
389 // ----------------------------------------
390 std::cout << std::endl;
391 std::cout << "Pass double param directly"
395 for(i = 0 ; i< nbLoop ; i++)
397 passDirectDouble (dx, dy);
402 // ----------------------------------------
404 std::cout << "Pass double param as ref"
408 for(i = 0 ; i< nbLoop ; i++)
410 passRefDouble (dx, dy);
414 // ----------------------------------------
416 std::cout << "Pass double param as ptr"
420 for(i = 0 ; i< nbLoop ; i++)
422 passPtrDouble (&dx, &dy);
428 // ====================================================================
430 std::cout << std::endl;
431 std::cout << "Exchange 2 scalars " << nbLoop << " times" << std::endl
432 << "==================="
433 << std::endl << std::endl;
435 // ----------------------------------------
437 std::cout << "Direct "<< std::endl;
440 for(i = 0 ; i< nbLoop ; i++)
451 // ----------------------------------------
453 std::cout << "Use a macro "<< std::endl;
456 for(i = 0 ; i< nbLoop ; i++)
463 // ----------------------------------------
465 std::cout << std::endl;
466 std::cout << "Use a function, param passed by reference" << std::endl;
469 for(i = 0 ; i< nbLoop ; i++)
476 // ----------------------------------------
478 std::cout << "Use a function, param passed by pointer" << std::endl;
481 for(i = 0 ; i< nbLoop ; i++)
488 // ----------------------------------------
490 std::cout << std::endl;
492 std::cout << "Use inline, .cxx-defined function, param passed by reference" << std::endl;
495 for(i = 0 ; i< nbLoop ; i++)
502 // ----------------------------------------
504 std::cout << "Use inline, .cxx-defined function, param passed by pointer" << std::endl;
507 for(i = 0 ; i< nbLoop ; i++)
514 // ----------------------------------------
516 std::cout << std::endl;
518 //To check the 2 following cases, we just put the two 'static' functions
519 //hifpswap and hNoifpswap in gdcmUtil.h
521 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer"
527 for(i = 0 ; i< nbLoop ; i++)
529 util.hifpswap (&a, &b);
535 // ----------------------------------------
537 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer"
541 for(i = 0 ; i< nbLoop ; i++)
543 util.hNoifpswap (&a, &b);
548 // ----------------------------------------
549 std::cout << std::endl;
551 std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer STATIC function"
555 for(i = 0 ; i< nbLoop ; i++)
557 gdcm::Util::sthifpswap (&a, &b);
562 // ----------------------------------------
564 std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer STATIC function"
568 for(i = 0 ; i< nbLoop ; i++)
570 gdcm::Util::sthNoifpswap (&a, &b);
575 //return 1; // will generate an error,
576 // just to allow us to see the full log in the dashboard