Web Log of Aleksey Nudelman: Thoughts on Software Architecture

Analysis and Review of Microsoft Technologies for IT Managers, Architects and Developers

Home » Archives » September 2004 » Benchmarking Java and C#: loop optimization is better in C#

[Previous entry: "XML Tutorial by Jaidev"] [Next entry: "Is Open Directory Really Open?"]

09/08/2004: "Benchmarking Java and C#: loop optimization is better in C#"


For a long time, I wanted to benchmark C# and Java. It is well known that loop optimization in .NET 1.X is inferiour to VC++ but how does C# (v1.1) compare to java 1.4.2.05?
Here is a test
//C# code:
using System;
class loop {
public static void Main() {
DateTime d=DateTime.Now;

int result=2;
for(long i=0;i<2100000000;i++)
{
for(int j=0;j<10;j++)
result=3;

}
DateTime d2=DateTime.Now;
Console.WriteLine((d2.Ticks-d.Ticks)/10000);
}
}

//Java code
class loop {
public static void main(String[] args) {
java.util.Date d = new java.util.Date();

int result=2;
for(long i=0;i<2100000000;i++)
{
for(int j=0;j<10;j++)
result=3;

}
java.util.Date d2=new java.util.Date();
System.out.println(d2.getTime()-d.getTime());
}
And the results are:
Java: 58.3 seconds
C#: 3.8 seconds
C# is much faster to execute. However, .NET loading time, not measured in this test, is always a few seconds longer than Java's.

  Home
  Archives
Phishing Inspector is  available

Copyright© 2006 Aleksey Nudelman