Resource Tests: Java vs Mono/.NET

As someone who has recently begun to experiment with the Linux operating system I have also been introduced to .NET’s Linux’s cousin Mono. This has made me question what the best cross-platform program language to use is. I am familiar with both Java and various .NET languages (Visual Basic & C#) so I decided to run a few tests to see what the resource usage on my Linux laptop is like between these two competing platforms.

Hardware

CPU Information

  • Processor (CPU): Intel Core2Duo CPU P8600 @ 2.40GHz

Memory Information

  • Total Memory (RAM): 4GiB
  • Swap Partition Size: ~6GiB

OS Information

  • OS: Linux 2.6.30.9-102.fc11.x86_64
  • System: Fedora  11
  • Desktop Environment: KDE 4.3.3

Display Info

  • Model: ATI Mobility Radeon HD 4670
  • Driver: 2.1.9026 FireGL

Hard Drive Info

  • Memory: 320GiB 7,200 RPM SATA

Software

I am using Java version 1.6.0 and Mono version 2.4.2.3 for these tests.

Test Setup

For the following tests I have provided source code for both the Java and the C# implementations. I then ran the programs and checked their CPU and memory usage.

Test I

Simple write/read test to the console. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestI {

     public static void main(String[] args) {

          System.out.println(“Hello World”);

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestI
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               Console.WriteLine(“Hello World!”);

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }

          }
     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test II

Simple primitive variable usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestII {

     public static void main(String[] args) {

          byte b = 0;
          short s = 0;
          int i = 0;
          long l = 0;
          float f = 0;
          double d = 0;
          boolean bo = true;
          char c = ‘a’;

          //set them all to something
          b = 1;
          s = 1;
          i = 1;
          l = 1;
          f = 1;
          d = 1;
          bo = false;
          c = ‘b’;

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }

     }

}

C# code:

using System;

namespace TestII
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               byte b = 0;
               short s = 0;
               int i = 0;
               long l = 0;
               float f = 0;
               double d = 0;
               bool bo = true;
               char c = ‘a’;

               //set them all to something
               b = 1;
               s = 1;
               i = 1;
               l = 1;
               f = 1;
               d = 1;
               bo = false;
               c = ‘b’;

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.1MiB
CPU Usage: 0% 0%

Test III

Simple primitive variable usage, this time with arrays. Same as above but with arrays of 10,000 for each primitive. The arrays were set using loops similar in structure to the following:

for(int x=0; x<10000; x++) {      b[x] = 1; } Results:

Java C#
Memory Usage: 5.9MiB 2.4MiB
CPU Usage: 0% 0%

Test IV

Simple object usage. CPU usage was recorded before the read.

Java code:

import java.io.IOException;

public class TestIV {

     public static void main(String[] args) {

          SimpleClass sc = new SimpleClass();

          sc.setID(5);
          sc.setName(“Hello World”);

          System.out.println(sc.getString());

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

public class SimpleClass {

     private int _ID;
     private String _name;

     public SimpleClass()
     {
          //default constructor
     }

     public SimpleClass(int ID, String name)
     {
          _ID = ID;
          _name = name;
     }

     public int getID()
     {
          return _ID;
     }

     public void setID(int ID)
     {
          _ID = ID;
     }

     public String getString()
     {
          return _name;
     }

     public void setName(String name)
     {
          _name = name;
     }
}

C# code:

using System;

namespace TestIV
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               SimpleClass sc = new SimpleClass();

               sc.setID(5);
               sc.setName(“Hello World”);

               Console.WriteLine(sc.getString());

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

using System;

namespace TestIV
{

     public class SimpleClass
     {

          private int _ID;
          private String _name;

          public SimpleClass()
          {
               //default constructor
          }

          public SimpleClass(int ID, String name)
          {
               _ID = ID;
               _name = name;
          }

          public int getID()
          {
               return _ID;
          }

          public void setID(int ID)
          {
               _ID = ID;
          }

          public String getString()
          {
               return _name;
          }

          public void setName(String name)
          {
               _name = name;
          }

     }
}

Results:

Java C#
Memory Usage: 5.6MiB 2.2MiB
CPU Usage: 0% 0%

Test V

Simple object usage, this time with arrays. Same as above but with arrays of 10,000 for each object. The arrays were set using loops similar in structure to test III:

Java code:

import java.io.IOException;

public class TestV {

     public static void main(String[] args) {

          SimpleClass[] sc = new SimpleClass[10000];

          for(int x=0; x<10000; x++)           {                sc[x] = new SimpleClass();                sc[x].setID(5);                sc[x].setName("Hello World");                System.out.println(sc[x].getString());           }           try {                System.in.read();           } catch (IOException e) {                e.printStackTrace();           }      } } C# code:

using System;

namespace TestV
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               SimpleClass[] sc = new SimpleClass[10000];

               for(int x=0; x<10000; x++)                {                     sc[x] = new SimpleClass();                     sc[x].setID(5);                     sc[x].setName("Hello World");                     Console.WriteLine(sc[x].getString());                }                try {                     Console.Read();                }catch(Exception e){                     Console.Write(e.StackTrace);                }           }      } } Results:

Java C#
Memory Usage: 10.1MiB 2.6MiB
CPU Usage: 8% 2%

Test VI

Everyone’s favourite bubble sort with 10,000 integers.

Java code:

import java.io.IOException;

public class TestVI {

     public static void main(String[] args) {

          int[] array = new int[10000];

          //fill array
          for(int i=0;i array[i+1])
                    {
                         int temp = array[i+1];
                         array[i+1] = array[i];
                         array[i] = temp;

                         swapped = true;
                    }
               }
          }while(swapped);

          System.out.println(“Sorted”);

          try {
               System.in.read();
          } catch (IOException e) {
               e.printStackTrace();
          }
     }

}

C# code:

using System;

namespace TestVI
{
     class MainClass
     {
          public static void Main(string[] args)
          {
               int[] arr = new int[10000];

               //fill array
               for(int i=0;i arr[i+1])
                         {
                              int temp = arr[i+1];
                              arr[i+1] = arr[i];
                              arr[i] = temp;

                              swapped = true;
                         }
                    }
               }while(swapped);

               Console.WriteLine(“Sorted”);

               try {
                    Console.Read();
               }catch(Exception e){
                    Console.Write(e.StackTrace);
               }
          }
     }
}

Results:

Java C#
Memory Usage: 6.3MiB 2.2MiB
CPU Usage: 6% 12%

All Results

Java C#
Test I Memory Usage: 5.6MiB 2.2MiB
Test I CPU Usage: 0% 0%
Test II Memory Usage: 5.6MiB 2.1MiB
Test II CPU Usage: 0% 0%
Test III Memory Usage: 5.9MiB 2.4MiB
Test III CPU Usage: 0% 0%
Test IV Memory Usage: 5.6MiB 2.2MiB
Test IV CPU Usage: 0% 0%
Test V Memory Usage: 10.1MiB 2.6MiB
Test V CPU Usage: 8% 2%
Test VI Memory Usage: 6.3MiB 2.2MiB
Test VI CPU Usage: 6% 12%

Conclusions

Well there you have it. It seems that at this point in time Mono is not only mature enough to compete against a seasoned veteran programming language like Java, but in some cases good enough to even supersede it. This is wonderful news for people looking for alternative cross-platform high level languages.

5 thoughts on “Resource Tests: Java vs Mono/.NET

  1. Those results look fairly clear cut to me. Add to that the fact that Mono has a graphical GUI editor, and is just as cross platform as Java, and I see it as a serious competitor.

  2. hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com/2009/09/c-array.html
    http://www.csharptalk.com/2009/10/creating-arrays.html

    simi

  3. Paul Hodgson

    Good work, nice to see a comparison of these languages conducted in something approaching a scientific manner. We’re all to quick get religious on these matters.

  4. bolovan

    Here ia a similar test on windows XP for java, mono, .net and C++:
    http://www.codeproject.com/KB/dotnet/RuntimePerformance.aspx
    Overall mono looks better than Java there.

    • Tyler Burton

      Thanks for the comment bolovan, those are some interesting benchmarks. Its fun to see how these languages handle various benchmark operations but its important to remember that these may not be indicative of real world performance. Heck the differences might not even be noticeable to the average user. Can you really tell the difference between 94ms and 15ms? Probably not. Can you tell the difference between 94ms and 93ms? Obviously not.

      The nice thing to see is that in general all of these languages are constantly improving in terms of performance (take Java 5 vs Java 6 as an example http://java.sun.com/performance/reference/whitepapers/6_performance.html). This lets developers pick languages that they like as opposed to picking languages because they have to just to get decent performance.

Comments are closed.