[vox-tech] benchmarking (again)

Peter Jay Salzman p at dirac.org
Fri Feb 17 08:13:42 PST 2006


I'm benchmarking doubles versus floats using C++, C, and java on Cygwin.

Here's the code for C++:


   // double.cc and double.c
   #define REPETITIONS 1000000

   int main( void )
   {
      for ( int i=0; i<REPETITIONS; ++i)
      {
         double epsilon = 1.0E30;
         double one     = 1.0;

         do {
            epsilon /= 2.0;
         } while ( one + epsilon > one );
      }
      return 0;
   }


   // float.cc and float.c
   #define REPETITIONS 1000000
   int main( void )
   {
      for ( int i=0; i<REPETITIONS; ++i)
      {
         float epsilon = 1.0E30F;
         float one     = 1.0F;

         do {
            epsilon /= 2.0F;
         } while ( one + epsilon > one );
      }

      return 0;
   }


which was compiled with:

   OPTIMIZE = -O3 -funroll-loops -fno-math-errno -fno-trapping-math \
      -fno-signaling-nans -funsafe-math-optimizations


and here's Double.java:

   class Double
   {
      public static void main( String[] args )
      {
         final int REPETITIONS=1000000;

         for ( int i=0; i<REPETITIONS; ++i)
         {
            double epsilon = 1.0E30;
            double one     = 1.0;

            do {
               epsilon /= 2.0;
            } while ( one + epsilon > one );
         }
      }
   }


with Float.java being defined similarly modulo some "F's".


Here are the results for the C++ (user time):

            double.cc   float.cc
            1.593       1.577
            1.608       1.608
            1.577       1.577
            1.499       1.515
            1.483       1.546
            1.561       1.546
            1.561       1.546
            1.561       1.499
            1.468       1.561
   average: 1.546       1.553
   std dev: 0.050       0.033

   high val 1.596       1.586
   low val  1.496       1.520

Although double beats out float, the difference is within experimental
error (the error bars for the timings overlap).


The timings for Float.java and Double.java are wierd...

   $ time java Float
   real    0m1.365s
   user    0m0.015s
   sys     0m0.061s

   $ time java Double
   real    0m1.318s
   user    0m0.015s
   sys     0m0.015s

What am I to make of the user time here?

And although I didn't show the "real" time for C++, the java real time is
lower than all the real times for the C++ trials.

How can this be?  I thought that java does stuff like initializing all basic
datatypes to "0" upon instantiation.  And I've read that java allocates
memory for all data items on the heap, which is never stored in L1 cache
while for C and C++, temporary objects are often put into the L1 cache.

How is java real time faster than C++ real time here?

Pete


More information about the vox-tech mailing list