From 3fd6e783a6b730143fdb374e1fd2c963eceb62a9 Mon Sep 17 00:00:00 2001 From: jpr Date: Fri, 21 Oct 2005 08:43:31 +0000 Subject: [PATCH] Add exInline.cxx, to 'visualize' the difference betwwen inline an not inline functions. Well... We see there is no difference ?!? --- Example/CMakeLists.txt | 6 +- Example/exInline.cxx | 212 ++++++++++++++++++ .../{TestPrintTime.cxx => exPrintTime.cxx} | 9 +- 3 files changed, 221 insertions(+), 6 deletions(-) create mode 100755 Example/exInline.cxx rename Example/{TestPrintTime.cxx => exPrintTime.cxx} (94%) mode change 100644 => 100755 diff --git a/Example/CMakeLists.txt b/Example/CMakeLists.txt index 4be063c8..0515c967 100644 --- a/Example/CMakeLists.txt +++ b/Example/CMakeLists.txt @@ -17,7 +17,9 @@ SET(EXAMPLE_SOURCES exExtractTag exSerieHelper exExtractDicomTags - + exPrintTime + exInline + #the following are utilities PrintDicomDir PrintFile @@ -43,7 +45,7 @@ SET(EXAMPLE_SOURCES ) FOREACH(name ${EXAMPLE_SOURCES}) - ADD_EXECUTABLE(${name} ${name}.cxx) + ADD_EXECUTABLE(${name} ${name}.cxx ) TARGET_LINK_LIBRARIES(${name} gdcm) INSTALL_TARGETS(/bin/ ${name}) ENDFOREACH(name) diff --git a/Example/exInline.cxx b/Example/exInline.cxx new file mode 100755 index 00000000..36683030 --- /dev/null +++ b/Example/exInline.cxx @@ -0,0 +1,212 @@ +/*========================================================================= + + Program: gdcm + Module: $RCSfile: exInline.cxx,v $ + Language: C++ + Date: $Date: 2005/10/21 08:43:31 $ + Version: $Revision: 1.1 $ + + Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de + l'Image). All rights reserved. See Doc/License.txt or + http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details. + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +// This test is expected to 'show' the actual effect on an 'inline' function. +// We exchange 2 numbers +// - with a macro : this is the quicker (anny 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() : absolutely NO effect ?!? +// - with an inline function described in the .h : absolutely NO effect ?!? +// +// Must we ask optimization to see the difference? + +#include "gdcmUtil.h" +#include "gdcmDebug.h" +#include + +#include +#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); + + +#define \ +mswap(a, b) \ +{ \ + tmp = a; \ + a = b; \ + b = tmp; \ +} + +void frswap(double &a, double &b) +{ + double tmp; + tmp = a; + a = b; + b = tmp; + +} + +void fpswap(double *a, double *b) +{ + double tmp; + tmp = *a; + *a = *b; + *b = tmp; + +} + +inline void ifpswap(double *a, double *b) +{ + double tmp; + tmp = *a; + *a = *b; + *b = tmp; +} + +inline void ifrswap(double &a, double &b) +{ + double tmp; + tmp = a; + a = b; + b = tmp; +} + +int main(int argc, char *argv[]) +{ + + int nbLoop; + if (argc > 1) + nbLoop = atoi(argv[1]); + else + nbLoop = 100000000; + unsigned int i; + clock_t r1, r2; + struct tms tms1, tms2; + + double a = 1, b = 2; + double tmp; + // ---------------------------------------- + + std::cout << "Use a macro "<< std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + mswap (a,b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; + + // ---------------------------------------- + + std::cout << "Use reference function" << std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + frswap (a,b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; + + // ---------------------------------------- + + std::cout << "Use pointer function" << std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + fpswap (&a, &b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; + + // ---------------------------------------- + + std::cout << "Use inline, main-defined reference function" << std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + ifrswap (a, b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; + + // ---------------------------------------- + + std::cout << "Use inline, main-defined pointer function" << std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + ifpswap (&a, &b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; + +/* +//To check the 2 following cases, 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; + } + + // ---------------------------------------- + + std::cout << "Use inline, .h defined, WITH inline keyword pointer function" + << std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + gdcm::Util::hifpswap (&a, &b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; + + + // ---------------------------------------- + + std::cout << "Use inline, .h defined, NO inline keyword pointer function" + << std::endl; + r1 = times(&tms1); + for(i = 0 ; i< nbLoop ; i++) + { + gdcm::Util::hNoifpswap (&a, &b); + } + r2 = times(&tms2); + std::cout + << (long) ((tms2.tms_utime) - (tms1.tms_utime)) + << std::endl; +*/ + +} diff --git a/Example/TestPrintTime.cxx b/Example/exPrintTime.cxx old mode 100644 new mode 100755 similarity index 94% rename from Example/TestPrintTime.cxx rename to Example/exPrintTime.cxx index a67cb203..6e05536f --- a/Example/TestPrintTime.cxx +++ b/Example/exPrintTime.cxx @@ -1,10 +1,10 @@ /*========================================================================= Program: gdcm - Module: $RCSfile: TestPrintTime.cxx,v $ + Module: $RCSfile: exPrintTime.cxx,v $ Language: C++ - Date: $Date: 2005/08/30 15:13:06 $ - Version: $Revision: 1.5 $ + Date: $Date: 2005/10/21 08:43:32 $ + Version: $Revision: 1.1 $ Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de l'Image). All rights reserved. See Doc/License.txt or @@ -29,7 +29,8 @@ #include // for std::ios::left, ... // Where is it? -//#define CLK_TCK 1 ///usr/include/bits/time.h:41: warning: this is the location of the previous definition +//#define CLK_TCK 1 ///usr/include/bits/time.h:41: +// warning: this is the location of the previous definition //Generated file: -- 2.45.1