Sunday, March 14, 2010

Java vs C# Performance Benchmarking

I'm a Java fan, but I use C# 99% in my job, so yesterday I was looking at a good EJB book (EJB 3 in action) while enjoying my coffee (the real one not the Java one ;).
I had a look at some EJB vs .NET benchmarks and I was shocked to see that .NET is much more faster than Java in the enterprise wild, so I decided to do some SIMPLE benchmarking myself.

Benchmarking Tests
I've wrote 3 really simple tests in both Java and C#, the first tests simply loops over an array 1000,000 times while doing some multiplication math, the second test loops over an array 1000,000 times too but while doing some more complex math, and finally the final tests that creates 1000,000 objects and saves them in an array.

I repeated each test 100 times, took the average of them as a final result, so I can be sure of the accuracy.



Benchmarking specs

DELL Latitude E5500 with this specs:
  • CPU: Intel Core 2 Duo T7250 @ 2.00 GHZ
  • RAM: 2GB
  • OS: Windows XP SP3.
  • C# 2008, .Net Version: 3.5 SP1
  • JDK 1.6 Update 18 
Benchmarking Results


As you can see from the charts, performance is much better in C# than Java, I don't know why there is this huge difference, especially in object creation test.

Here is the sources I used to do the test, try them guys on your computers and let us know the results.

Java Sources

Main.java




 6 package benchmarking;
 7 
 8 public class Main {
 9 
10     public static final int TEST_COUNT = 100;
11     public static final int LOOP_COUNT = 1000000;
12     public static void startTests() {
13       int i = 0;
14       long start, end, diff, sum = 0;
15 
16       System.out.print("Array 1 ===> ");
17       while (i < TEST_COUNT){
18             start = System.currentTimeMillis();
19             BM.arrayTest1(LOOP_COUNT);
20             end = System.currentTimeMillis();
21             diff = end - start;
22             sum += diff;
23             //System.out.println(diff + " ms");
24             i++;
25       }
26       System.out.println(sum/TEST_COUNT + " ms");
27       i = 0;
28 
29 
30       
31       System.out.print("Array 2 ===> ");
32       while (i < TEST_COUNT){
33             
34             start = System.currentTimeMillis();
35             BM.arrayTest2(LOOP_COUNT);
36             end = System.currentTimeMillis();
37             diff = end - start;
38             sum += diff;
39             //System.out.println(diff + " ms");
40             i++;
41       }
42       System.out.println(sum/TEST_COUNT + " ms");
43       i = 0;
44 
45       System.out.print("Creating objects ===> ");
46       while (i < TEST_COUNT){
47 
48             start = System.currentTimeMillis();
49             BM.arrayTest2(LOOP_COUNT);
50             end = System.currentTimeMillis();
51             diff = end - start;
52             sum += diff;
53             //System.out.println(diff + " ms");
54             i++;
55       }
56       System.out.println(sum/TEST_COUNT + " ms");
57       i = 0;
58 
59     }
60 
61 
62     public static void main(String[] args) {
63        startTests();
64     }
65 
66 }

BM.java





 1 package benchmarking;
 2 
 3 public class BM {
 4     public static void arrayTest1(int x) {
 5         int i = 0;
 6         double[] r = new double[x];
 7         while (i < x){
 8             r[i] = i*i;
 9             i++;
10         }
11     }
12     
13     public static void arrayTest2(int x) {
14         int i = 0;
15         double[] r = new double[x];
16         while (i < x){
17             r[i] = Math.sin(i) + Math.cos(i) + Math.sqrt(i);
18             i++;
19         }
20     }
21 
22     public static void createObjects(int x) {
23         int i = 0;
24         Object[] r = new Object[x];
25         while (i < x){
26             r[i] = new Object();
27             i++;
28         }
29     }
30 
31 
32 }
C# Sources

Program.cs





1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Diagnostics;
    6 
    7 namespace BenchMarking
    8 {
    9     class Program
   10     {
   11         const int TEST_COUNT = 100;
   12         const int LOOP_COUNT = 1000000;
   13         static void doTests()
   14         {
   15             int i = 0;
   16             long diff, sum = 0;
   17             Stopwatch sw = new Stopwatch();
   18 
   19             Console.Write("ARRAY 1 ===> ");
   20             while (i < TEST_COUNT)
   21             {
   22 
   23                 sw.Start();
   24                 BM.arrayTest1(LOOP_COUNT);
   25                 sw.Stop();
   26                 diff = sw.ElapsedMilliseconds;
   27                 sw.Reset();
   28                 //Console.WriteLine(diff + " ms");
   29                 sum += diff;
   30                 i++;
   31 
   32             }
   33             Console.WriteLine(sum / TEST_COUNT + " ms");
   34             sw.Reset();
   35             i = 0;
   36 
   37             Console.Write("ARRAY 2 ===> ");
   38             while (i < TEST_COUNT)
   39             {
   40 
   41                 sw.Start();
   42                 BM.arrayTest2(LOOP_COUNT);
   43                 sw.Stop();
   44                 diff = sw.ElapsedMilliseconds;
   45                 sw.Reset();
   46                 //Console.WriteLine(diff + " ms");
   47                 sum += diff;
   48                 i++;
   49             }
   50             Console.WriteLine(sum / TEST_COUNT + " ms");
   51             sw.Reset();
   52             i = 0;
   53 
   54 
   55             Console.Write("CREATE OBJECTS ===> ");
   56             while (i < TEST_COUNT)
   57             {
   58 
   59                 sw.Start();
   60                 BM.createObjects(LOOP_COUNT);
   61                 sw.Stop();
   62                 diff = sw.ElapsedMilliseconds;
   63                 sw.Reset();
   64                 //Console.WriteLine(diff + " ms");
   65                 sum += diff;
   66                 i++;
   67             }
   68             Console.WriteLine(sum / TEST_COUNT + " ms");
   69             sw.Reset();
   70             i = 0;
   71 
   72 
   73         }
   74 
   75 
   76         static void Main(string[] args)
   77         {
   78             doTests();
   79         }
   80     }

BM.cs




1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 
    6 namespace BenchMarking
    7 {
    8     class BM
    9     {
   10         public static void arrayTest1(int x)
   11         {
   12             int i = 0;
   13             double[] r = new double[x];
   14             while (i < x)
   15             {
   16                 r[i] = i * i;
   17                 i++;
   18             }
   19         }
   20 
   21         public static void arrayTest2(int x)
   22         {
   23             int i = 0;
   24             double[] r = new double[x];
   25             while (i < x)
   26             {
   27                 r[i] = Math.Sin(i) + Math.Cos(i) + Math.Sqrt(i);
   28                 i++;
   29             }
   30         }
   31 
   32         public static void createObjects(int x)
   33         {
   34             int i = 0;
   35             object[] r = new object[x];
   36             while (i < x)
   37             {
   38                 r[i] = new object();
   39                 i++;
   40             }
   41         }


I'd  like to suggest more and more tests, and to widen the test to Linux, if you have any more suggesions please post them so I can include them in here.

Happy Coding.







1 comment:

Anonymous said...

As you know, one of the strong points of Java is the community and all if the works done that are available for free. Java's basic math functions are very slow but there are some amazing math libraries that would be interesting to view compared to your results. Try Jama http://math.nist.gov/javanumerics/jama/ or Colt http://acs.lbl.gov/~hoschek/colt/ or Jscience http://jscience.org/