VB,Java,Fortran or C?

Hi

I did a very simple loop in all these languages in order to test which was fastest. The i-loop contained:

i=1 to 500,000,000(that's 5e8)

x=x+i

y=x/i

z=i^30

next i

Not a particularly brilliant test, but I couldn't find any benchmarking programs on the web, and I only really know VB and Java. Anyway, the speeds in seconds for this loop where:

Visual Basic: 314 seconds (i.e. over 5 mins!)

Java: 37 secs

Fortran: 29 secs but 5 secs when optimised

C: 6 secs but less than 2 secs when optimised

The code for these is shown at the end.

I have a 4000 line code in VB, but it takes forever to run (I leave it overnight, but it's still not enough). There's no graphics or anything - just simulating trading on 10 years worth of stock price data, which does not involve anything particularly complex (arrays, if-statements, simple maths, and a bit of sorting - just I do it all a few thousand times).

I'm now thinking of converting it all into C because, according to that simple test, VB is over 150 times slower than C, though I'd really rather do it in Java and introduce my colleagues to the benefits of Java (got this job about six months ago, sort of my first programming job). In a previous discussion (that Why is Java slow? with about 160 replies), someone said VB was only 50% slower than C, and 75% slower than Java, but then someone else said VB could be done faster than Java?

Does anyone out there know of any proper benchmarking tests I can run, and if in "proper" programs VB is really that bad when compared to C, and is it possible to get Java down to a speed close to C if all it's doing is mostly just maths and no graphics? Basically, is it worth my while converting it in the first place, and will it be just as fast in Java as in C and save me the bother of learning C?

Any pointers to further info where speed tests like these have been performed, or any light you can shed on this matter, would be greatly appreciated.

Cheers,

Gregory.

Code Tests

-

VB was version 6.0

Java was latest 1.3.1 jdk

Fortan77 and C used the gnu compilers (from www.cygwin.com)

Machine: 1GHz Pentium III running Windows NT 4.0

***VB

Private Sub Command1_Click()

Dim i As Long

Dim x As Double

Dim y As Double

Dim z As Double

x = 0#

y = 0#

z = 0#

For i = 1 To 500000000

x = x + i

y = x / i

z = i ^ 30

Next i

frmMain.Caption = "Finished, x = " & x

End Sub

***Java

public class speed_test{

public static void main(String args[]){

double x=0.0;

double y=0.0;

double z=0.0;

for(int i=0; i<500000000; i++){

x=x+i;

y=x/i;

z=i^30;

}

System.out.println("Finished, x = " + x);

}

}

***FORTRAN77

program speed_test

integer i

double precision x, y, z

x=0.0

y=0.0

z=0.0

do 20 i=1, 500000000

x=x+i

y=x/i

z=i**30

20continue

write(6,*)"Finished, x = ", x

stop

end

***C

main () {

int i;

double x;

double y;

double z;

x=0.0;

y=0.0;

z=0.0;

for(i=0; i<500000000; i++)

x=x+i;

y=x/i;

z=i^30;

printf("Finished, x = %f\n", x);

}

[3459 byte] By [gregbeekman] at [2007-9-26 11:20:47]
# 1

I suspect you've done the same mistake in both your C and your Java programs. None of these languages have a built-in operator for raising x to power n (I suppose that's what you mean by i^30). Instead, "^" means "exclusive binary OR" or something like that in both C and Java. So if you really need to raise numbers into arbitrary powers, or into fractional powers, you'll have to use some special library math functions in both C and Java. If you do that, you may find that your Fortran program becomes the fastest - Fortran is really best optimised for math...

As for speeding up Java - you may try to run HotSpot JVM with the non-default JIT compiler using "-server" option and see if it helps. This compiler, unlike the default "-client" one, performs more optimisations and thus produces faster code. I am not sure, though, that it will be very evident with your "micro-benchmark".

count_mdm at 2007-7-2 0:27:25 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

I don't think you can get as quick as C with Java. This because Java is non-platform specific.

If you want a near C performance without having to learn it, then use Java. However, if you want performance at any costs you should take C (or Fortran).

If you find that some parts of the program are to slow (critical zones), you can make some specific modules with another langage (in a .exe file executed externally or with JNI coding to connect your app) to gain performance.

That doesn't really help, but I it is the most precise answer I can tell.

xeneve at 2007-7-2 0:27:25 > top of Java-index,Java HotSpot Virtual Machine,Specifications...