Performance Evaluation on Java Thread and .NET Thread

01 Mar 2011 by

Performance evaluation on Java single thread and multi-thread has already been discussed in the previous post. So I will show you all the performance of .NET thread comparable to Java thread. We did the same scenario and experimental environment as my last post. The overall Java performs much better than .NET both single thread and multi-thread.

Objective

To evaluate the performance of Java and .NET particularly in thread.

Methodology

As before, we develop a benchmark for calculation of Fibonacci with a value of 45. We run 5 times for each indicator. The indicators are Java Single Thread, Java Mutli-Thread, .NET Single Thread and .NET Multi-Thread.

Experimental Environment

Parameter Value
Operating System Windows 7 Ultimate x64
JDK Version 1.6u21
CPU Intel Core 2 Duo P9500 @ 2.54GHz
Memory 4GB

Result

Java Thread and .NET Thread

From the figure we can see that the only Java multi-thread needs shortest time to calculate meaning best performance among other else. .NET single thread seems to take longer to complete the calculation. An interesting here is that in .NET multi-thread takes a bit longer than even Java single thread. So this result shows that Java performs better in any sense compare to .NET.

Believe it or not? test by yourself. I attach the code as below.

Source Code

The source of java can be found here. In .NET, I select C# as a language tool.

C# Single Thread

class Program
{
  static void Main(string[] args)
  {
    int startTime = System.Environment.TickCount;
    for(int i=0; i<5; i++)
    {
        Console.WriteLine("Fib=" + Fibonacci(45));
    }
    int stopTime = System.Environment.TickCount;

    Console.WriteLine("Main finish duration=" + (stopTime-startTime));
    Console.ReadLine();
  }

  private static long Fibonacci(int n)
  {
    if (n == 0) return 0L;
    if (n == 1) return 1L;

    return (Fibonacci(n - 1) + Fibonacci(n - 2));
  }
}

C# Mutli-Thread

class Program
{
  static void Main(string[] args)
  {
    long startTime = System.Environment.TickCount;
    Thread[] t = new Thread[5];
    for (int i = 0; i < 5; i++)
    {
        t[i] = new Thread(RunFibonacci);
        t[i].Start();
    }

    for(int i=0; i<5; i++)
    {
        t[i].Join();
    }
    long stopTime = System.Environment.TickCount;

    Console.WriteLine("Duration=" + (stopTime-startTime));
    Console.ReadLine();
  }

  private static void RunFibonacci()
  {
    var fib = Fibonacci(45);
    Console.WriteLine("Fibonacci=" + fib);
  }

  private static long Fibonacci(int n)
  {
    if (n == 0) return 0L;
    if (n == 1) return 1L;

    return (Fibonacci(n - 1) + Fibonacci(n - 2));
  }
}

Conclusion

If we write program properly, Java may run faster as this article stated. I look forward to seeing the performance evaluation of both programming platform in Core-i3, Core-i5 and Core-i7.

No responses yet

Leave a Reply