5 ms·
I'd like to say something that may sound a bit controversial, but bear with me: for most practical programs, Java's performance beats C++; in fact, it beats C.
by pron 13y ago
I'd like to say something that may sound a bit controversial, but bear with me: for most practical programs, Java's performance beats C++; in fact, it beats C. And assembly. There, I've said it, and now let me try to back this up.
Proving my claim is very hard, but I'll try to persuade you. The task is made harder by the fact that for every possible Java program, there exists a C++ program that performs the exact same task at least as fast as Java (proof by example: the JVM).
And yet, Java is the best performing programming environment in existence (well, in widespread use; there may be runtimes I'm not aware of). How is that possible? When we say that one language or environment is faster than another, do we mean that there exists a string of characters in that language that would produce a faster executable? If that is what we mean, then yes, C++ is faster than Java. But I think the question is not whether that desired program exists, but whether you can write it. To that question, I think the answer is no in most circumstances, especially when it comes to large programs.
Why? because when you write a large, hopefully maintainable, C++ program, you must make use of language features that are hard for the compiler to optimize, most notably -- virtual methods. The JVM JIT does runtime compilation, so it inlines virtual calls, and de-optimizes them if its assumptions are shown to be false. Method inlining is the most important compiler optimization because it opens to door to all kinds of other optimizations like lock elision, scalarization of objects and more.
And there's another issue. What do we mean by performance nowadays? It's not too hard for a single-threaded C++ program to beat a similar Java program (provided that the program is not large, and does not make use of virtual methods). But modern hardware is multi- and many-core, and most scalable lock-free algorithms absolutely require a good GC in order to function well. Java probably has (several of) the most advanced GC(s) in wide use.
To sum up: C++ can beat Java in small benchmarks, but Java will beat C/C++ in most large, real-world programs; and if the program is heavily multi-threaded, Java has even more of an edge.
- beagle3 13y agoTaking your logic a little further, Python+PyPy beats Java hands down. Every single argument applies to Python vs. Java or Python vs. C/C++ more than it does to Java vs. C/C++ Which, incidentally, is my tool of choice... Personally, I've never met nice readable Java code. I'm sure it exists somewhere, but I've actually gone looking for it in the past (including posting a question on HN, out of 10 responses only one was concrete -- Play Framework -- and they've abandoned Java since). Every piece of Java I've met is an overengineered piece of AbstractAbstractFactoryFactoryBeanSingleton class. I'm sure Notch writes better code. But as far as I can tell he's the only one.
- noelwelsh 13y agoPython, and PyPy AFAIK, doesn't provide true concurrent threads or lock-free primitives like CAS. This means there are a class of high performance and high concurrency applications you simply can't write in Python. I'm no fan of Java, but I am a fan of the JVM. The two don't necessarily go together, as I'm sure you know. I also recognise the JVM has limitations. There will (hopefully) be better things in the future. This is one reason that Rust is interesting to me, but it has a long way to go before I'd use it in production.
- pron 13y ago> Python+PyPy beats Java hands down. . Every single argument applies to Python vs. Java or Python vs. C/C++ more than it does to Java vs. C/C++ Not at all. Python does not even begin to apply a minuscule portion of the optimizations the JVM performs. Also, I wasn't talking theory, I was talking practice: I've worked on porting two multi-million LOC soft real-time applications from C++ to Java, and a few smaller programs; big performance improvements in all cases. I was simply explaining why Java performs better in practice, not speculating why it would. But if you haven't seen that with your own eyes, all that's left for me is to explain why this is so. > Personally, I've never met nice readable Java code. This has no relevance to the discussion, though there's plenty of nice, readable Java code. Anyway, I was talking about the JVM, not specifically Java the language. > I'm sure Notch writes better code. But as far as I can tell he's the only one. Right... and as far as I can tell, you're not really into Java :) That's okay. Millions of developers are.
- beagle3 13y ago> Not at all. Python does not even begin to apply a minuscule portion of the optimizations the JVM performs Are you familiar with PyPy? It doesn't go as far as the JVM, but "minuscule" is definitely wrong. > I was talking practice: I've worked on porting two multi-million LOC soft real-time applications from C++ to Java, and a few smaller programs; big performance improvements in all cases. My experience is the opposite. Not two million lines - just a few tens of thousands, but still. > This has no relevance to the discussion, though there's plenty of nice, readable Java code. Anyway, I was talking about the JVM, not specifically Java the language. One of the previous posts (to which I was replying) claimed that Java is faster in practice because (paraphrasing) it is nicer to read and write, and that was a response to that. > Right... and as far as I can tell, you're not really into Java :) That's okay. Millions of developers are. That's good, that guarantees I'll always be in demand :)
- nivertech 13y agoI think your logic is flawed. I will agree with it if you will replace "for most practical programs" by "for most in-house or agency-coded enterprise applications". Another point is that Java bytecode doesn't have SIMD primitives, so unless JIT can do SIMD-optimizations, any SIMD-aware C/C++ compiler will beat Java. This is especially relevant for the kind of task this article discusses: PFOR Delta integer compression.
- pron 13y agoSIMD for Java is a long time coming. It doesn't change my statement though, because obviously, you can write small snippets of code, or benchmarks, where C beats Java. I was saying that for a large program (and there are few large programs whose performance depends on SIMD), Java usually beats C. Actually, there are other changes that would improve Java performance more than SIMD, like value types. The lack of value types causes many unnecessary cache faults in Java programs.
- beagle3 13y ago> I was saying that for a large program (and there are few large programs whose performance depends on SIMD), Java usually beats C. That goes counter to my experience. Care to share your anecdata here?
- pron 13y agoI thought I did. I've been involved with porting large C++ programs to Java (mostly soft real-time defense applications). We've always seen a significant performance increase (not due to newer hardware). Like I said, this happens for two reasons: large maintainable programs require virtual methods that are hard for the C++ compiler to optimize, and Java opens a whole new realm of possibilities with scalable non-blocking data structures. I would say this is true for most large applications, but untrue in particular circumstances: Java does not excel in many cases of numeric computation (where value types, and, yes, SIMD, would be of assistance). Also, Java is the wrong choice if startup time is important.
- ratt 13y agoNo, you don't need a GC to implement lock-free data-structures. RCU allows you to generically defer work on a piece of data until it is no longer accessible. I personally find the idea behind it to be very elegant and I'd recommend reading up on it. While it isn't as simple as a full-blown GC, once you have a working implementation that suits your needs (eg. urcu), it's usually pretty straightforward to introduce in a lock-free data-structure and has far less overhead. In fact, when it comes to real-time systems (eg. games), the GC is usually something you want to avoid at all costs. Note that there are various other tricks (eg. hazard pointers, reference counters, etc.) to get around the same problem but they're mostly specialized to various situations. As an example, last time I checked gcc's std::shared_ptr implementation on x86, it used some lock-free magic to implement its thread-safe reference counter. In fact, I think it could even be used to build entire lock-free data-structures and never have to worry about deleting anything.
- pron 13y agoSeveral things: First, various techniques for avoiding GC in lock-free data structures are not in wide use and for good reason: they are far more complicated to implement in a general fashion than you think. Sometimes they're very specific, sometimes they require cooperation by the kernel, and sometimes you end up writing a buggy and inefficient garbage collector just to get around implementation problems. Java's lock-free data structures are used everywhere today and with great results. Second, regarding real-time. There are hard real-time GCs for Java, and they work great, only they tend to cost a lot of money. But hard real time systems often sacrifice performance for the sake of worst-case guarantees -- that's a whole other set of tradeoffs. In general, and certainly when it comes to throughput, a good GC is both the easiest and the most efficient way to manage memory. Finally, because, as I've shown, for every Java program there exists a C++ program that performs as efficiently or better, you could, given enough effort, always surpass Java. In practice, doing that is extremely hard (unless it's a very specific, well understood problem), and the returns are hardly worth the effort. What you gain in integer compression you lose in method invocation. What you gain with SIMD you lose on malloc. What you gain in cache-friendliness you lose in multi-threading. And the gap is only narrowing in Java's favor so that there are fewer and fewer things you gain when using C++. This is not to say that C++ doesn't have it's place. First, you need it to write the JVM :) You need it in a resource constrained environment where memory is limited, and you may not want to spare a thread for concurrent GC. You need it when startup time is crucial. You need it (or Fortran) for some well-understood numeric computing problems. But for general, large, long-running server side applications - Java is king. And BTW, games are years, if not a decade or more, behind advances in software engineering (I've had some experience in that). In the past decade they've literally progressed on one front only (though, an important one): graphics and GPGPU (for physics and AI). I know that some AAA studios literally forbid any multithreaded code on the CPU (beyond a simple and predefined division of labor among a few threads). MMO game engines, for examples, are practically primitive compared to advanced server-side systems in other markets.