X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=Testing%2FTestInline.cxx;h=a11eaff85a6cee5744b10a0b0551c5e2d1a7bac3;hb=df1540af8230b8ca5e3cba23e9127e2a2428df78;hp=60bbc61864840e2581e3ef0d6bf9828cf72f3f70;hpb=ee38a24e454e9675ee479469a844f8a850bc1afa;p=gdcm.git diff --git a/Testing/TestInline.cxx b/Testing/TestInline.cxx index 60bbc618..a11eaff8 100755 --- a/Testing/TestInline.cxx +++ b/Testing/TestInline.cxx @@ -3,8 +3,8 @@ Program: gdcm Module: $RCSfile: TestInline.cxx,v $ Language: C++ - Date: $Date: 2005/10/24 01:16:20 $ - Version: $Revision: 1.2 $ + Date: $Date: 2007/10/30 09:07:46 $ + Version: $Revision: 1.17 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -20,7 +20,7 @@ // - with a macro : this is the quicker (any doubt ?) // - with a function, passing the params by pointer // - with a function, passing the params by reference (exactly same time) -// - with an inline function described in the main() +// - with an inline function described in the .cxx // absolutely NO effect ?!? // - with a function, described in the .h // absolutely NO effect ?!? @@ -30,22 +30,40 @@ // Which CXX_FLAGS, LINKER_FLAGS, ..., must we set to see the difference? #include "gdcmUtil.h" -#include "gdcmDebug.h" -#include +#if defined(__BORLANDC__) || defined (_MSC_VER) #include -#ifdef CMAKE_HAVE_SYS_TIMES_H + #if defined(__BORLANDC__) + #include + #include + #endif +#define GET_TIME(a) a=clock() +#define HOW_LONG(b,a) \ + std::cout << (double) (b-a) << std::endl + +#else #include +#define GET_TIME(a) times(&a) +#define HOW_LONG(b,a) \ + std::cout \ + << (long) ((b.tms_utime) - (a.tms_utime)) \ + << std::endl #endif +#include + void frswap (double &a, double &b); void fpswap (double *a, double *b); inline void ifrswap(double &a, double &b); inline void ifpswap(double *a, double *b); -uint16_t passDirect(uint16_t a, uint16_t b); -uint16_t passRef(uint16_t &a, uint16_t &b); -uint16_t passPtr(uint16_t *a, uint16_t *b); +uint8_t passDirect8(uint8_t a, uint8_t b); +uint8_t passRef8(uint8_t &a, uint8_t &b); +uint8_t passPtr8(uint8_t *a, uint8_t *b); + +uint16_t passDirect16(uint16_t a, uint16_t b); +uint16_t passRef16(uint16_t &a, uint16_t &b); +uint16_t passPtr16(uint16_t *a, uint16_t *b); uint32_t passDirect32(uint32_t a, uint32_t b); uint32_t passRef32(uint32_t &a, uint32_t &b); @@ -55,43 +73,22 @@ double passDirect(double a, double b); double passRef(double &a, double &b); double passPtr(double *a, double *b); -/* -//To perform a full check, just put the two 'static' functions -//hifpswap and hNoifpswap in a .h - - static inline void hifpswap(double *a, double *b) - { - double tmp; - tmp = *a; - *a = *b; - *b = tmp; - } - - static void hNoifpswap(double *a, double *b) - { - double tmp; - tmp = *a; - *a = *b; - *b = tmp; - } - -*/ - -#define \ -mswap(a, b) \ -{ \ - tmp = a; \ - a = b; \ - b = tmp; \ +#define \ +mswap(a, b) \ +{ \ + double tmp = a;\ + a = b; \ + b = tmp; \ } +// ============= no inline + void frswap(double &a, double &b) { double tmp; tmp = a; a = b; b = tmp; - } void fpswap(double *a, double *b) @@ -100,9 +97,10 @@ void fpswap(double *a, double *b) tmp = *a; *a = *b; *b = tmp; - } +// ============= inline + inline void ifpswap(double *a, double *b) { double tmp; @@ -134,15 +132,41 @@ uint32_t passDirect32(uint32_t a, uint32_t b) } -uint16_t passRef(uint16_t &a, uint16_t &b) +uint16_t passRef16(uint16_t &a, uint16_t &b) +{ + return a + b; +} +uint16_t passPtr16(uint16_t *a, uint16_t *b) +{ + return *a + *b; +} +uint16_t passDirect16(uint16_t a, uint16_t b) +{ + return a + b; +} + +uint8_t passRef8(uint8_t &a, uint8_t &b) +{ + return a + b; +} +uint8_t passPtr8(uint8_t *a, uint8_t *b) +{ + return *a + *b; +} +uint8_t passDirect8(uint8_t a, uint8_t b) +{ + return a + b; +} + +float passRefFloat(float &a, float &b) { return a + b; } -uint16_t passPtr(uint16_t *a, uint16_t *b) +float passPtrFloat(float *a, float *b) { return *a + *b; } -uint16_t passDirect(uint16_t a, uint16_t b) +float passDirectFloat(float a, float b) { return a + b; } @@ -163,250 +187,397 @@ double passDirectDouble(double a, double b) int TestInline(int argc, char *argv[]) { +#if defined(__BORLANDC__) || defined (_MSC_VER) + clock_t tms1, tms2; +#else + struct tms tms1, tms2; +#endif + +// ==================================================================== + + std::cout << std::endl << std::endl + << "Just to be sure : sizes of native types" << std::endl + << "=======================================" + << std::endl << std::endl; + // just to know, on every proc + std::cout << "Size of char " << sizeof(char) << std::endl; + std::cout << "Size of short int " << sizeof(short int) << std::endl; + std::cout << "Size of int " << sizeof(int) << std::endl; + std::cout << "Size of long " << sizeof(long) << std::endl; + std::cout << "Size of float " << sizeof(float) << std::endl; + std::cout << "Size of double " << sizeof(double) << std::endl; + std::cout << std::endl; + std::cout << "Size of char* " << sizeof(char*) << std::endl; + std::cout << "Size of short int*" << sizeof(short int*)<< std::endl; + std::cout << "Size of int* " << sizeof(int*) << std::endl; + std::cout << "Size of double* " << sizeof(double*) << std::endl; + std::cout << "-----------------" << std::endl; + + // ==================================================================== + unsigned int nbLoop; unsigned int i; if (argc > 1) nbLoop = atoi(argv[1]); else - nbLoop = 100000000; + nbLoop = 100000000; - clock_t r1, r2; - struct tms tms1, tms2; - + uint8_t x8 =1, y8 =2; + uint16_t x16=1, y16=2; + uint32_t x32=1, y32=2; + float fx =1.0f, fy=1.0f; + double dx =1.0 , dy=1.0; double a = 1, b = 2; - double tmp; - uint16_t x, y; + // ==================================================================== + + std::cout << std::endl << std::endl + << "Check different ways of passing scalars to a function "<< nbLoop << " times" << std::endl + << "=====================================================" + << std::endl << std::endl; + + std::cout << "Pass uint_8 param directly" + << std::endl; + + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + passDirect8 (x8, y8); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - std::cout << "Use a macro "<< std::endl; - r1 = times(&tms1); + std::cout << "Pass uint_8 param as ref" + << std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - mswap (a,b); + passRef8 (x8, y8); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; - + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - std::cout << "Use reference function" << std::endl; - r1 = times(&tms1); + std::cout << "Pass uint_8 param as ptr" + << std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - frswap (a,b); + passPtr8 (&x8, &y8); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; - + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - - std::cout << "Use pointer function" << std::endl; - r1 = times(&tms1); + std::cout << std::endl; + std::cout << "Pass uint_16 param directly" + << std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - fpswap (&a, &b); + passDirect16 (x16, y16); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; - + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - std::cout << "Use inline, main-defined reference function" << std::endl; - r1 = times(&tms1); + std::cout << "Pass uint_16 param as ref" + << std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - ifrswap (a, b); + passRef16 (x16, y16); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; - + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - std::cout << "Use inline, main-defined pointer function" << std::endl; - r1 = times(&tms1); + std::cout << "Pass uint_16 param as ptr" + << std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - ifpswap (&a, &b); + passPtr16 (&x16, &y16); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); // ---------------------------------------- - -//To check the 2 following cases, we just put the two 'static' functions -//hifpswap and hNoifpswap in gdcmUtil.h - - std::cout << "Use inline, .h defined, WITH inline keyword pointer function" + std::cout << std::endl; + std::cout << "Pass uint_32 param directly" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - gdcm::Util::hifpswap (&a, &b); + passDirect32 (x32, y32); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); - // ---------------------------------------- - - std::cout << "Use inline, .h defined, NO inline keyword pointer function" + + std::cout << "Pass uint32_t param as ref" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - gdcm::Util::hNoifpswap (&a, &b); + passRef32 (x32, y32 ); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; - + GET_TIME(tms2); + HOW_LONG(tms2,tms1); // ---------------------------------------- - std::cout << "Pass uint_16 param directly" + std::cout << "Pass uint_32 param as ptr" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passDirect (x, y); + passPtr32 (&x32, &y32); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + + // ---------------------------------------- + std::cout << std::endl; + std::cout << "Pass float param directly" + << std::endl; + + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + passDirectFloat (fx, fy); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); // ---------------------------------------- - std::cout << "Pass uint_16 param as ref" + std::cout << "Pass float param as ref" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passRef (x, y); + passRefFloat (fx, fy); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); // ---------------------------------------- - std::cout << "Pass uint_16 param as ptr" + std::cout << "Pass float param as ptr" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passPtr (&x, &y); + passPtrFloat (&fx, &fy); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- + std::cout << std::endl; + std::cout << "Pass double param directly" + << std::endl; + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + passDirectDouble (dx, dy); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); // ---------------------------------------- + + std::cout << "Pass double param as ref" + << std::endl; - uint32_t m =1, n=2; - std::cout << "Pass uint_32 param directly" + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + passRefDouble (dx, dy); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- + + std::cout << "Pass double param as ptr" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passDirect32 (m, n); + passPtrDouble (&dx, &dy); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + +// ==================================================================== + + std::cout << std::endl; + std::cout << "Exchange 2 scalars " << nbLoop << " times" << std::endl + << "===================" + << std::endl << std::endl; + // ---------------------------------------- - std::cout << "Pass uint32_t param as ref" - << std::endl; - r1 = times(&tms1); + std::cout << "Direct "<< std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passRef32 (m, n); + double tmp; + tmp=a; + a=b; + b=tmp; } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - std::cout << "Pass uint_32 param as ptr" - << std::endl; - r1 = times(&tms1); + std::cout << "Use a macro "<< std::endl; + + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + mswap (a,b); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + + // ---------------------------------------- + + std::cout << std::endl; + std::cout << "Use a function, param passed by reference" << std::endl; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passPtr32 (&m, &n); + frswap (a,b); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + + // ---------------------------------------- + + std::cout << "Use a function, param passed by pointer" << std::endl; + GET_TIME(tms1); ; + for(i = 0 ; i< nbLoop ; i++) + { + fpswap (&a, &b); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- + std::cout << std::endl; - double dx=1.0, dy=2.0; + std::cout << "Use inline, .cxx-defined function, param passed by reference" << std::endl; + + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + ifrswap (a, b); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); - std::cout << "Pass double param directly" + // ---------------------------------------- + + std::cout << "Use inline, .cxx-defined function, param passed by pointer" << std::endl; + + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + ifpswap (&a, &b); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + + // ---------------------------------------- + + std::cout << std::endl; + +//To check the 2 following cases, we just put the two 'static' functions +//hifpswap and hNoifpswap in gdcmUtil.h + + std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer" << std::endl; - r1 = times(&tms1); + + gdcm::Util util; + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passDirectDouble (dx, dy); + util.hifpswap (&a, &b); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + // ---------------------------------------- - - std::cout << "Pass double param as ref" + + std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passRefDouble (dx, dy); + util.hNoifpswap (&a, &b); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); // ---------------------------------------- - - std::cout << "Pass double param as ptr" + std::cout << std::endl; + + std::cout << "Use inline, .h defined, WITH inline keyword, param passed by pointer STATIC function" << std::endl; - r1 = times(&tms1); + + GET_TIME(tms1); for(i = 0 ; i< nbLoop ; i++) { - passPtrDouble (&dx, &dy); + gdcm::Util::sthifpswap (&a, &b); + } + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + + // ---------------------------------------- + + std::cout << "Use inline, .h defined, NO inline keyword, param passed by pointer STATIC function" + << std::endl; + + GET_TIME(tms1); + for(i = 0 ; i< nbLoop ; i++) + { + gdcm::Util::sthNoifpswap (&a, &b); } - r2 = times(&tms2); - std::cout - << (long) ((tms2.tms_utime) - (tms1.tms_utime)) - << std::endl; + GET_TIME(tms2); + HOW_LONG(tms2,tms1); + //return 1; // will generate an error, + // just to allow us to see the full log in the dashboard return 0; }