locked
Is .NET's application bit faster than java's application ? RRS feed

  • Question

  • Hi,

    if anybody has idea about java, can explain this confusing topic :  .NET's application run's bit faster that java's application, if we provide same environment and equivalent code .

    I am confused please explain something.

    Thursday, September 23, 2010 6:35 PM

Answers

  • This is a rather meaningless "benchmark" to test.  Here, all you're really testing is the garbage collection performance of both runtimes - and even then, I suspect you're not using good timing methodologies.  Running your sample C# code, in a release (optimized) build, outside of Visual Studio, I get a runtime of 3.1633619 seconds... I suspect you're seeing differences in the debugger more than differences in the runtimes.

     

    That being said, I firmly believe, for "real" programs, Java and C# (.NET) will be very similar in terms of performance.  The underlying API and framework tools you use will have a much, much more profound effect on your overall speed in any "real" program.  If you're making a program with a user interface, the UI sits idle most of the time, anyways.


    Use the language and tool that best fits your needs, and for which you are the most comfortable.  I'd focus more on questions like:

     

    1) Which framework provides me the functionality I need

    2) Which framework is the easiest to understand, and has the best documentation

    3) Which language is more productive for my development

    4) What are my options in terms of toolsets for debugging, tracking, profiling, and maintaining my code

     

    Those are the questions that will make a difference over time - subtle differences in performance really don't matter 99.99% of the time - and when they do, you'll probably want to write your code in C++ in order to get access to things like SSE and the like, which are typically not exposed to managed code.

     

    (As for your specific question about Java's virtual vs. C#'s non-virtual - this is very minor, and wouldn't show up at all in your benchmark above - you'd need to be calling methods on objects regularly to see it.  Personally, I think the fact that C# has true, stack-based value types is probably a bigger advantage to C# vs. Java.  However, the Java JITs tend to do more optimizations than the .NET JIT, so many benchmarks show Java having some advantages for code that I wouldn't expect...)


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, September 24, 2010 4:15 PM

All replies

  • They're using different runtimes.  Each will be optimized for its own tasks, and Java and .NET provide a different form of instruction codes.

     

    There are some cases where java is faster than .NET code, for the equivelent code.  However, both are reasonably fast - focus on whether it's fast enough for your use case, and pick the technology that is best suited for your project.  (I, personally, prefer developing in C# and .NET because I prefer the libraries and development environment - the speed of the runtime should not be an issue, as long as it's "fast enough" for your use case...)

     


    Reed Copsey, Jr. - http://reedcopsey.com
    Thursday, September 23, 2010 6:42 PM
  • They're using different runtimes.  Each will be optimized for its own tasks, and Java and .NET provide a different form of instruction codes.

     

    There are some cases where java is faster than .NET code, for the equivelent code.  However, both are reasonably fast - focus on whether it's fast enough for your use case, and pick the technology that is best suited for your project.  (I, personally, prefer developing in C# and .NET because I prefer the libraries and development environment - the speed of the runtime should not be an issue, as long as it's "fast enough" for your use case...)

     


    Reed Copsey, Jr. - http://reedcopsey.com


    Actually , 2 days back i had attended a Seminar on comparison between .NET and Java application.There they said that .NET application is faster than Java application. Reason behind Java's slow performance as they said is that every Method in Java is bydefault Virtual.They can be overridden.For this Java uses double pointer, so using double pointer bydefalut in any case makes Java application bit slower( as they said). 

     To test the truth of there statement i wrote two equivalent codes.One in C# and one in Java, and ran.

    But alas!! the same equivalent codes took different time to run.C# code took approx 23 sec. while Java code took 13 sec. A Huge difference.This seems contrary to the person who gave me such information in seminar. Still i have not reached to any point whether they were right or wrong.

     I want opinion from your side.

     

    C# code:

    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          DateTime dt = new DateTime();
          dt = DateTime.Now;
          DateTime dt1 = new DateTime();
     
          for (int i = 0; i < 999999999; i++)
          {
            A a = new A();
          }
          dt1 = DateTime.Now;
     
          TimeSpan dtDiff = dt1 - dt;
          Console.Write(dtDiff.ToString());
          Console.Read();
        }
      }
      class A
      {
        int x;
        int y;
      }
    }
    

    java code:

    package javaapplication2;
    import java.util.*;
    public class Main {
     public static void main(String[] args) {
    Calendar calendar1 = Calendar.getInstance();
    Date time1 = calendar1.getTime();
    System.out.println(time1);
     for(long i=0;i<999999999;i++)
     {
      A a=new A();
     }
    Calendar calendar2 = Calendar.getInstance();
    Date time2 = calendar2.getTime();
    System.out.println(time2);
      }
    }
    class A
    {
    int x;
    int y;
    }
    

     

     

    Friday, September 24, 2010 1:09 AM
  • This is a rather meaningless "benchmark" to test.  Here, all you're really testing is the garbage collection performance of both runtimes - and even then, I suspect you're not using good timing methodologies.  Running your sample C# code, in a release (optimized) build, outside of Visual Studio, I get a runtime of 3.1633619 seconds... I suspect you're seeing differences in the debugger more than differences in the runtimes.

     

    That being said, I firmly believe, for "real" programs, Java and C# (.NET) will be very similar in terms of performance.  The underlying API and framework tools you use will have a much, much more profound effect on your overall speed in any "real" program.  If you're making a program with a user interface, the UI sits idle most of the time, anyways.


    Use the language and tool that best fits your needs, and for which you are the most comfortable.  I'd focus more on questions like:

     

    1) Which framework provides me the functionality I need

    2) Which framework is the easiest to understand, and has the best documentation

    3) Which language is more productive for my development

    4) What are my options in terms of toolsets for debugging, tracking, profiling, and maintaining my code

     

    Those are the questions that will make a difference over time - subtle differences in performance really don't matter 99.99% of the time - and when they do, you'll probably want to write your code in C++ in order to get access to things like SSE and the like, which are typically not exposed to managed code.

     

    (As for your specific question about Java's virtual vs. C#'s non-virtual - this is very minor, and wouldn't show up at all in your benchmark above - you'd need to be calling methods on objects regularly to see it.  Personally, I think the fact that C# has true, stack-based value types is probably a bigger advantage to C# vs. Java.  However, the Java JITs tend to do more optimizations than the .NET JIT, so many benchmarks show Java having some advantages for code that I wouldn't expect...)


    Reed Copsey, Jr. - http://reedcopsey.com
    Friday, September 24, 2010 4:15 PM