my crude benchmark results

John Gabriele john3g@bestweb.net
Tue Jul 13 04:29:00 GMT 2004


A friend was just asking me about speed when using Java
for trig, so I put together a small program to get some
*very* crude benchmark results. See the end of this email
for the source code text. It's in C as well as Java. I'm on
Mac OS X at the moment (a 500 MHz G3 with 256 MB),
and I compared the C program with Apple's JVM as well as
with GCJ 3.4.0.
I realize that such a contrived program can't possibly
give results that are scalable to large production software,
and further, that developer time for said large program
is likely far less than for a similar C or C++ program. However,
here are my results anyway (in case they might be of any
interest to anyone here).
I ran each program multiple times in a row (without touching
the rest of the operating system) and got a nice average for
each. Ok,.. heaven forgive me for the cycles I am about to burn...
Time taken to run:
 in C:
 8.5 seconds
 Java via Apple's JVM:
 22.2 seconds
 Java via GCJ:
 25.5 seconds
Of course, considering that GCJ is Free software, and the large
number of platforms it runs on, I'm quite happy with it. Still though,
it's surprising how much faster C is than Java for this simple
procedural code.
Though, as an aside, I should mention that I'm absolutely blown away
at how blindingly fast computers are these days in general (taking 
sines,
cosines, arcsines, arccosines, and squares of 50000 doubles, done 100
times, in under half a minute. *Yow* that's friggin fast. Whew! [wipes
sweat off forehead]).
Do you folks on GNU/Linux and MS Windows get about the same sort
of ratios?
=============== snip ================
/* For rand(). */
#include <stdlib.h>
/* For sin(), cos(), asin(), acos(), and pow(). */
#include <math.h>
/* For printf(). */
#include <stdio.h>
int main()
{
 const int SIZE = 50000;
 double nums[SIZE];
 double sines[SIZE];
 double cosines[SIZE];
 double arcsines[SIZE];
 double arccosines[SIZE];
 double squares[SIZE];
 double sum = 0;
 const int TIMES = 100;
 int i = 0, j = 0, k = 0, m = 0, n = 0, p = 0;
 printf( "Size = %d\n", SIZE );
 for ( i = 0; i < SIZE; ++i )
 {
 nums[i] = (((double)rand()) / ((double)(RAND_MAX))) * 3.14159 / 
2.0;
 }
 for ( j = 0; j < TIMES; ++j )
 {
 for ( k = 0; k < SIZE; ++k )
 {
 sines[k] = sin( nums[k] );
 cosines[k] = cos( nums[k] );
 squares[k] = pow( sines[k], 2 ) + pow( cosines[k], 2 );
 }
 }
 for ( m = 0; m < TIMES; ++m )
 {
 for ( n = 0; n < SIZE; ++n )
 {
 arcsines[j] = asin( sines[j] );
 arccosines[j] = acos( cosines[j] );
 if ( arcsines[j] - arccosines[j] > 0.1 )
 {
 printf( "We're boned!" );
 return;
 }
 }
 }
 /* Now sum the elements of squares[], and we should get back
 something close to SIZE. */
 for ( p = 0; p < SIZE; ++p )
 {
 sum += squares[p];
 }
 printf( "Sum = %f", sum );
 return 0;
}
=============== snip ================
=============== snip ================
public class Main
{
 public static void main( String[] args )
 {
 final int SIZE = 50000;
 double [] nums = new double[SIZE];
 double [] sines = new double[SIZE];
 double [] cosines = new double[SIZE];
 double [] arcsines = new double[SIZE];
 double [] arccosines = new double[SIZE];
 double [] squares = new double[SIZE];
 double sum = 0;
 final int TIMES = 100;
 System.out.println( "Size = " + SIZE );
 for ( int i = 0; i < SIZE; ++i )
 {
 nums[i] = Math.random() * 3.14159 / 2.0;
 }
 for ( int i = 0; i < TIMES; ++i )
 {
 for ( int j = 0; j < SIZE; ++j )
 {
 sines[j] = Math.sin( nums[j] );
 cosines[j] = Math.cos( nums[j] );
 squares[j] = Math.pow( sines[j], 2 ) + Math.pow( 
cosines[j], 2 );
 }
 }
 for ( int i = 0; i < TIMES; ++i )
 {
 for ( int j = 0; j < SIZE; ++j )
 {
 arcsines[j] = Math.asin( sines[j] );
 arccosines[j] = Math.acos( cosines[j] );
 if ( arcsines[j] - arccosines[j] > 0.1 )
 {
 System.out.println( "We're boned!" );
 return;
 }
 }
 }
 /* Now sum the elements of squares[], and we should get back
 something close to SIZE. */
 for ( int i = 0; i < SIZE; ++i )
 {
 sum += squares[i];
 }
 System.out.println( "Sum = " + sum );
 }
}
=============== snip ================
---J


More information about the Java mailing list

AltStyle によって変換されたページ (->オリジナル) /