I'll have a go at putting some of the code provided into a more
reasonable benchmarking framework, and we'll see what the results are.
Java certainly *isn't* 2-3 times slower than C# - but there are some
*very* bad benchmarks out there.
Okay, I've got the nested loop test in a better shape. This one showed
CPU of 0.28 for C# and 0.85 for Java on the web page, so C# appears to
be three times faster, right? Well, let's try a more balanced test...
Java code:
public class NestedLoop
{
public static void main(String[] args)
{
int iterations = Integer.parseInt(args[0]);
int loop = Integer.parseInt(args[1]);
long total = 0;
long start = System.currentTimeMillis();
for (int i=0; i < iterations; i++)
{
total += test(loop);
}
long end = System.currentTimeMillis();
System.out.println ("Total time (ms): "+(end-start));
System.out.println ("Running total: "+total);
}
public static int test(int n)
{
int x=0;
for (int a=0; a<n; a++)
for (int b=0; b<n; b++)
for (int c=0; c<n; c++)
for (int d=0; d<n; d++)
for (int e=0; e<n; e++)
for (int f=0; f<n; f++)
x++;
return x;
}
}
C# code:
using System;
public class NestedLoop
{
public static void Main(string[] args)
{
int iterations = int.Parse(args[0]);
int loop = int.Parse(args[1]);
long total = 0;
DateTime start = DateTime.Now;
for (int i=0; i < iterations; i++)
{
total += Test(loop);
}
DateTime end = DateTime.Now;
Console.WriteLine ("Total time (ms): "+
(end-start).TotalMilliseconds);
Console.WriteLine ("Running total: "+total);
}
public static int Test(int n)
{
int x=0;
for (int a=0; a<n; a++)
for (int b=0; b<n; b++)
for (int c=0; c<n; c++)
for (int d=0; d<n; d++)
for (int e=0; e<n; e++)
for (int f=0; f<n; f++)
x++;
return x;
}
}
Points to note:
1) My results are taken given suitably large numbers, so that the Java
JIT has time to "warm up" (i.e. re-optimise), and so that timing
granularity is irrelevant (so it doesn't matter that I'm not using
StopWatch in .NET).
2) We're only testing the time take to do the actual looping (and
method call) rather than including the startup time (which I believe to
be the main cause of bias in the web results)
3) We're measuring elapsed time rather than CPU time. However, the
tests were conducted on an idle box (admittedly Vista, which is never
completely idle!) and shot up to 100% during the tests - I don't think
the difference would be significant.
So, results on my laptop (C# 2, .NET 3, Java 1.6):
C:\Users\Jon\Test>java NestedLoop 1000 15
Total time (ms): 27188
Running total: 11390625000
C:\Users\Jon\Test>nestedloop 1000 15
Total time (ms): 37154.8485
Running total: 11390625000
In other words, C#: 37s, Java: 27s.
Instead of Java being shown to be 3 times *slower* in this test, it's
actually 25% faster.
I don't plan to repeat this for all the other tests on the web page,
but hopefully that shows you just how much stock you should put in
their testing methodology.