]> Creatis software - gdcm.git/commitdiff
Add exInline.cxx, to 'visualize' the difference betwwen inline an not inline
authorjpr <jpr>
Fri, 21 Oct 2005 08:43:31 +0000 (08:43 +0000)
committerjpr <jpr>
Fri, 21 Oct 2005 08:43:31 +0000 (08:43 +0000)
functions.
Well...
We see there is no difference ?!?

Example/CMakeLists.txt
Example/exInline.cxx [new file with mode: 0755]
Example/exPrintTime.cxx [moved from Example/TestPrintTime.cxx with 94% similarity, mode: 0755]

index 4be063c8f8d2de735e7e068eea5d548c4389a11d..0515c9675e27fab522c1366054d7ad9ae30f239d 100644 (file)
@@ -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 (executable)
index 0000000..3668303
--- /dev/null
@@ -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 <iostream>
+
+#include <time.h>
+#include <sys/times.h>
+
+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; 
+*/ 
+
+}
old mode 100644 (file)
new mode 100755 (executable)
similarity index 94%
rename from Example/TestPrintTime.cxx
rename to Example/exPrintTime.cxx
index a67cb20..6e05536
@@ -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 <iomanip> // 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: