Is the coffee cup getting colder? (Part 1)

Filed under: by: PritinTheGreat


Is Java better than .NET? Yes? No? Maybe? Well, it's quite futile to argue about this issue without strong reasoning. Instead, Piyush Sahay (piyushsahay@live.in) and I decided to find out for ourselves what difference there actually is between the two.

In today's Part-1 of our series of tests between Java and .NET , we see two tests. Since the C# compiler ( and every other supported language's compiler) converts the written program into MSIL (Microsoft Intermediate Language), it's perfectly safe to say that we're comparing the JVM and .NET Framework (and not Java and C#). Had the same code been written in VB or VC++, the same intermediate code would have been generated, and a very similar test result would have been obtained.

We use .NET 3.5 SP1 and Java SE 6 for our comparisons. We used both Java and C# command line compilers. All tests were performed with the same laptop (rather slow, but that doesn't make a difference in this case) running Windows XP Service Pack 3.

TEST 1
LOOPING AND PRINTING TILL 1,00,000
------------------------------------------------

The objective of this test was very elementary. The written program had to loop from 0 to 1,00,000 and print every number obtained, in a new line. Sounds simple, no? Let's have a look at the programs we used to do the same.

JAVA Program (extract)


for(double x = 0; x <= 100000 ; x++)
{
System.out.println(x);
}


C#.NET Program (extract)

for(double x = 0; x <= 100000 ; x++)
{
Console.WriteLine(x);
}

The following are the obtained results : (lesser is better)




.NET 3.5 SP1 : 11714 ms
Java SE 6 : 14797 ms

Clearly, when it comes to looping operations ( we also tried a while loop ), the .NET Framwork is much more effective and faster. The difference is about 3 seconds.

One point to be noted though, is that when NetBeans is used to "compile and run" the program, the execution time was significantly lower than the above shown results. In my opinion, this can be credited to the fact that NetBeans does not start a console (cmd.exe) as a child process, which could include additional overheads. There is a subwindow within the NeatBeans IDE that is used to display the output instead.


TEST 2
BUBBLE SORT OF 1000 ELEMENTS (WORST CASE)
------------------------------------------------------------

Bubble sort is usually the choice of many programmers when it comes to sorting a list of a small number of elements. We tried it with 1000 elements. Obviously, 1000 elements doesn't even vaguely fit into the "Small list" category, but still it has a healthy (healthy?) number of if statements, and thus proves to be a valid measure of speed. The time complexity of the bubble sort is n2 (n - square). This means, that a list of 1000 elements will need up to 1000000 if statements. This can be easily deduced from the fact that there is a nested for loop with an if statement. The maximum number of comparisons are required when the list to sorted is in it's "worst state". That is, the list is in perfectly descending order, and our program tries to sort it in ascending order.

Let's have a look at this code.

Java Program (extract)

int i,j,temp,a[]=new int[1000];
long start,end,time;
for (i=999; i>=0; i--)
a[i]=1000-i;
for (i=0; i<1000;>
for (j=0; j<999-i;>
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

C#.NET Program (Extract)

int i,j,temp;
int [] a=new int[1000];
long start,end,time;
for (i=999; i>=0; i--)
a[i]=1000-i;
for (i=0; i<1000;>
for (j=0; j<999-i;>
if (a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

The following are the obtained results. (lesser is better)





.NET 3.5 SP1 : 8 ms
Java SE 6 : 15 ms

This proves that conditional statements are executed almost 100% faster in .NET as in Java. Though this cannot be used as an absolute proof, it is indicative.

In our forthcoming tests, we will be posting comparisons based on more complex algorithms and concepts. Till then the thing to do is to Live and Love .NET !

Credits : The title to this series of posts was sugested by Piyush "The Java Boss" Sahay.

6 comments:

On April 5, 2009 at 9:56 AM , Piyush said...

Interesting results, the sad part ofcourse is that .NET is stuck to Windows.
Java performance in Linux systems is much faster.
Also.... java IDEs score over Visual Studios (as the tests had pointed out)
The Space complexity part of the analysis is yet to be put up.
So relax guys... THE COFFEE CUP IS STILL HOT!

 
On April 5, 2009 at 10:59 AM , PritinTheGreat said...

Note to all... IceMan is piyushsahay@live.in ..
The guy who suggested the title for this series.

Anyway, as you said... the space part is indeed left.

 
On April 6, 2009 at 12:07 AM , Merijn said...

Most probably with IO-bound cases as in the first case, almost all time is spent by the text display and font rendering engines.
Furthermore, you'd never ever implement your own sorting routine in 2009 anymore; you either use a sorted datastructure, or use sorting routines given by your platform.

 
On April 13, 2009 at 9:17 AM , Prasanth said...

well .NET is a resource bug jus like the space bug of our class...
it is still bound to windows platforms, it tried a lot through Xbox 360 and HD-DVD but still it can not perform as good as JAVA usage in a BD disk player and many other hand held devices.

if it is for a fancy windows machine U can go ahead, if it is anywhere outside windows/microsoft "there's a long long way to go"...

and that bubble sort implementation is highly inefficient way of coding, it can be done in n!...

 
On April 13, 2009 at 9:26 AM , PritinTheGreat said...

Well Prasanth, there is a reason why I chose the "inefficient bubble sort". I don't really care about the bubble sort. It's the time taken for a lot of conditional and looping statements to execute, that I want to check.

And as far as .NET being bound to Windows is concerned, it is indeed true for the time being; however many recent developments such as Mono, may change this; though as you said, this may take a long time.

Do enlighten me on what a "fancy windows machine" is.

 
On April 13, 2009 at 9:59 AM , Prasanth said...

fancy windows machine like PDA's running on windows mobile OS and also desktops like vista.
.NET micro framework weighs 300+kb while JAVA ME is around 160kb, and the base ram requirements are also high for .NET micro...

try to find the number of .NET managed components/entities in a VISTA OS...
U'll understand that even microsoft itself does not trust much on their own products for their OS.