Java versus C/C++ performance – which is really faster?

Cliff Click of Azul Systems has an excellent post on Java vs C/C++ performance:

Is Java faster than C/C++?  The short answer is: it depends.

He then presents three categories of cases: the first C/C++ beats Java, the second where Java beats C/C++, and the third and longest, where C/C++ proponents claim Java is slower but in reality it is not.

My quick summary: there are few cases where Java (or C#) is so much slower that it matters, save for one big issue which Click mentions early on – start-up time:

Flash games beat Java games mostly because it took 30+sec to load the JVM from disk… and so now the web-game developer community has settled on Flash as the standard (and it still takes 10+sec to load the JVM).

Start-up time makes a big difference to usability, for reasons which I cannot entirely explain. Just for fun, I’ve been running Microsoft Office 95 in a virtual instance of Windows XP recently, and the fact that Word 95 loads in a blink makes it feel much faster than Word 2010, which takes 5-10 seconds on first load, even though the productivity difference must be negligible.

If I had to theorise about this, I’d say it is to do with the way humans learn from experience. We don’t like waiting, and if an application take a while to start then part of our brain tells us to avoid it in future. We override that instinct when it is an app we need to run, but it contributes to a negative impression.

The opposite is also true. If an application starts instantly – I mean to the point where we can use it, not just a splash screen – it contributes to a positive impression and we are more inclined to use it in future.

Incidentally, Click thinks that Java is generally faster than C#, though he adds that he is “not able to give C# a fair treatment” because he does not track it closely.

9 thoughts on “Java versus C/C++ performance – which is really faster?”

  1. It seems he hasn’t been using profile guided optimization for C/C++, it pretty much makes any C++ program optimize paths where the JIT could optimize slightly better. Also he seems to be using GCC, where he might want to look into the intel compiler for performance.

    I think it is a moot point though, just ask your own gut feeling, have you ever encountered a fast JAVA program, especially a desktop one? This isn’t totally the language/JVM’s fault though, sometimes the harder something is the more skill the use has to have, this skill it translates across the board… Performance is not just about how many CPU cycles your program uses, actually it has very little to do with that, at least if you talk about UI programs. If you talk servers CPU is just one of your limited resources, IO, caches memory/network stacks etc also needs to be considered.

    I don’t understand the interest in micro benchmarks and that they somehow would translate in a language being better performing in a large system. Performance has a lot more to do with your architecture and design than your choice of language.

    C# wins hands down for desktop program due to its NGEN, not because it is so much better, just because the programs load snappier, also the CLR seems better at handling memory for desktop usage than the JVM which seems highly geared towards server loads. The UI model is also geared specifically towards windows whereas the Java tries to be both (SWT is .. well)

    It seems JAVA/C# tries hard to convince the world that they have no downside and that C/C++ is complex. It is surely not the language that makes the difference on the complexity of the program, at least not a program/system of any sizeable sort. And for the contrived examples in the article would make C++ program fly by as they would unroll loops properly (I am saying this without trying though..).

    I heavily use C++, C#, powershell, Java and ruby I could rate them here but the post would be very long, but I will say this, which probably will be long too:

    You get the control you need and mostly more than you need, there are absolutely no road blocks in your way, or things you can’t do because some language designer didn’t like them (unsigned in Java anyone?, function pointers…). In my own experience you pay more attention to details important for good system designs in C++. You care about your resource management, because you must, you care about life cycles because you must, this leads to a tight program just because it has to work well. Flip side is, if you are programming in C++ and don’t care about this you will fail miserably whereas a program in Java could be sloppily written but still function. It isn’t really harder to code in C++ as a language than any other.

    To create C++ program definitely requires more skill than get the same thing done in Java/C#/VB. Note I am not saying that it doesn’t require skill to create a _good_ Java/C#/VB program, just that the initial effort/knowledge barrier to get going is much lower.

    Generally C++ development in Linux is a pain due to unnecessary complex tooling, in VS C++ is fast, refactoring sub-par, debugging and tools are good (purify, quantify, valgrind etc). Crash dump handling excellent. The opposite is true though, if your main goal is to develop fast and have enough resources to run it with enough performance than C++ will be overly powerful for the task. If your program does a lot of string handling C++ is not the choice I would make.

    My general experience is, when I have written a C++ program it works, its performance is rarely a problem and with any sounds memory management story it will be stable. With a sound platform story portability is excellent, write once compile anywhere really mostly is true, and if your application is layered properly (which it has to be, remember the details matter in C++) you will get the full benefits of the platform while sacrificing little in portability.

    Java program are generally slightly faster to write, but I generally have to spend more time after they are written to make them perform well enough, not hog resources like mad and if they are desktop program it is a real challenge to keep the memory working set down. Especially problematic is when you develop on the JDK but run your code on for instance classpath/cacao/jamvm, now tooling support because less bright and compile once run anywhere definitely gets a new face. Java loses many benefits of the platform as they really want you to run in their model.

    C# Same problem as java, performance can be a problem, but generally plays nicer with the OS. Mono is a great effort, but java supports more platforms. C# exposes the platform if you want to access it, it is a really interesting middle path between sandboxed Java and full power C++.

    Finally doing web development in C++ is just plain..silly. This is where Java/C# really shines, mostly due to its better support for dynamic behaviors and string handling.

    There is nothing you can’t do in C++ that you can do you Java/C#, but the opposite isn’t true at least not without extensive effort (mostly because all CPUs come with an assembler not a JVM/CLR..).

    Java web start is excellent, C++ has nothing to offer and C# has click once which is also excellent (although in my opinion slightly more complex than web start, but at the same time also more powerful). C++ program can be bundle in webstart/clickonce of course, so in that sense you can get the benefits for C++ too.

  2. Niclas

    Thanks for the comment; very useful especially as you have experience across the various languages, tools and platforms.

    Tim

  3. I find the claim that Java supports more platform than Mono incorrect.

    Sun officially supports Linux on x86, x86-64 and Itanium. So does Mono.

    Sun officially supports Solaris with x86, x86-64 and SPARC. So does Mono.

    Sun officially supports Windows with x86, x86-64 and Itanium. Mono only supports x86 and x86-64.

    Then third parties support their own licensed versions of Java on their platform, Apple OSX comes to mind and IBM with their own ports.

    But Mono in addition supports:
    x86 and x86-64:
    FreeBSD and OpenBSD

    PowerPC:
    Linux/PowerPC
    Wii

    PowerPC64:
    Linux
    PlayStation 3
    XBox360

    S390 and s390/X (31 and 64 bit systems from IBM)

    ARM:
    Linux
    iPhoneOS (All 3 CPUs used in iPhone/iPad/iPod)

    SPARC:
    In addition, we support Linux/SPARC

    Just like Java has been licensed and ported to vendor specific systems, Mono has too. In some cases the ports never became public (IBM has done two ports to AIX that they have never released to the public for instance) or maintained by third parties (MIPS 32 and MIPS 64 for Linux).

    We had an Alpha port, but it died due to Alpha going the way of the dodo.

  4. Oh, and I forgot Android.

    Mono supports Android out of the SVN tree today.

  5. @Niclas

    > Finally doing web development in C++ is just plain..silly.

    I do not agree. I have written a perfectly fast web application with html/ajax on client and C++ on server
    and users are pretty happy with it. In my opinion, this is a common misperception that C++ is not good for
    web development because of the lack of ready-to-go tools.

  6. Glebas:

    As I said you can do anything in C++, same is not enitrely true for C#/Java. I am a better C++ programmer than Java/C#, but I still find it much faster to get things done when it comes to web development in Java/C#. I might not have had the right tools or the correct approach though, but that’s my experience. Proper unicode handling cross platform has been one of my issues, the lack of GC another as many patterns you (or I) want to use heavily depend on functions being able to return new objects.

  7. Miguel de Icaza:

    You know the capabilities of Mono way better than I do, so whom am I to argue, I only know what I see (and I despertely wish to try it out), I guess that might mean that more advertising of mono capabilities to world is in order (or at least in my world =).

    Officially Java does not support more platforms I agree, but there are so many implementation of java engines that you could probably have one for each platform in existence today =). A multitude of mobile (non PDA) support java out of the box in one form or another, mono/C# isn’t there yet to my knowledge, but you would certainly know better =). It has never crossed my mind to try Mono/C# on non PDA platforms, so if so java got its message out there, while mono hasn’t, at least within my earshot.

    Mono has an official linux ppc port including JIT? Last time I checked (4 weeks ago, now that C# 3.0 is more or less supported it has gotten very intesting). I got the impression it was in some limbo state, I probably should have asked in IRC, but the forums were so silent about ppc. If it is actually fully supported I would be more than willing to try it out, we are using ELDK 2.1, so I guess gcc 2.95 will pose a problem.. But upgrading to ELDK 4.1 would potentially solve that. We are using a MPC5200 CPU in this case btw.

    Also due to the multitude of JVM:s it is often easy to find one to stick on your embedded environment, especially since many support interpretet mode.

    Does mono still maintain its interpreter?

    I never said much about C#, but since it is you, I will mention that I find C# a much more interesting language than Java, also the code usually looks prettier, at least to my eyes, and it has alot more language features. C# seems to have been designed with a pragmatic design view, while Java seems to have been designed with a academic mindset, where purity is more important than usefulness/day to day practical use. I feel more trapped in Java than C#, by far. I also find mono one of the most interesting open source efforts at the moment, I wish Microsoft would support it more and enhance the platform story for C#/.Net and get the word buzzing even more.

    If you maintain that mono has a good platform story for embedded devices noone would be happier than I, I have been trying (well, more wanting but not had time to really try it out) to get mono onto our embedded platform to see how it fares against our jvm.

    To my defence I have rarely used mono, when we do C# it is windows only so we stick to the MS .Net, however obvisouly we would like to try to have some code reuse going, and IKVM might be interesting to get a mix.

  8. Java versus C/C++ performance
    Comparing language for performance is “stupid” as its not compareable
    why u ask ? well they produce different thing

    Mono is interesting
    It’s a fruit for the love of c#
    too bad some interesting stuff, mono touch and their future of android, is costly $_$
    @Miguel
    perhaps some free business model Miguel ?
    Develop with mono with no cost, with shared revenue for limited number of sales if it pass n sales ?
    for someone like me, who can’t afford developer tools until the apss sells well

Comments are closed.