4 ms·
Gosling was fond of speculating that Java would eventually be faster than C and C++... but nothing ever materialized or made enough of a difference. Uhm, I'd s
by pron 14y ago
Gosling was fond of speculating that Java would eventually be faster than C and C++... but nothing ever materialized or made enough of a difference.
Uhm, I'd say Java already is faster than C++ for all intents and purposes. If your codebase is >50KLOC and the number of developers on your team is more than, say, 4 or 5, then some crazy optimizations that are possible in C++ for small applications are out of the question, and the JIT plus the GC more than make up the remaining difference. Also, modern performance-critical software requires good concurrency, and Java has stuff like fork-join, that I'm not sure is available in standard C++ (it is, of course, available in Cilk).
So, even though it's hard to provide hard evidence (because most benchmarks are <50KLOC and written by one or two developers at most), I'd say from experience that Java already is faster than C++ in practically all circumstances (except, maybe, scientific-computation applications), not counting startup time.
The only missing piece in the Java performance puzzle is value types and arrays-of-structs, and those, I believe are coming, and should make scientific-computation faster in Java as well.
- codewright 14y agoI'll assume you're talking about throughput, rather than latency/real-time constraints. I'm pretty dubious, given that Google extensively utilizes C++ in their more performance sensitive portions of their architecture. Their custom web server, for example, is written in C++. Lets not forget the rather large HPC codebases out there in Fortran. Can you prove any of this?
- pron 14y agoObviously, Java is less predictable than C++ so it's not suitable for hard real-time (though RTSJ JVMs are!), but I'd say that in the common case, Java would have lower latencies, too (when everything is compiled after a good warmup). I don't know why Google uses C++, but I'd guess that it's for predictability. A web server would rather always have a reasonably low latency than somewhat lower latencies in most cases with a chance for an occasional latency spike. Also, small, well-contained C++ program would probably perform better than Java, and I don't know how big is each of Google's C++ apps. And like I said, this is very hard to prove; it's just been my experience after replacing a couple of legacy C++ apps with Java ones. But I think it could be "theoretically proven" (if that even means anything for such an empirical claim), by analyzing why and where C++ has any performance advantages vs. where Java has them, and showing that the larger the codebase and the team, the C++ advantages must disappear, while Java's become more significant. Just to give two examples of why that should be: in a large application, the domain model might require some class methods to be virtual. The JVM can detect at runtime that in practice only one or two implementations are present, and would inline them. This is not possible for the C++ compiler. The other example is memory management. Java's memory allocation is so much faster than malloc (it's just a pointer bump), allows for better concurrency, and automatically reduces fragmentation. The way to get good memory management in C++ is by forsaking malloc for manual management (say, object pools), but that becomes increasingly difficult to maintain in a large, ever shifting codebase with many developers involved. Also, Java has better concurrency support which is, as you've said, important for throughput. The only counter example is arrays of structs that are simply not supported on the JVM, and where C++ would have obvious cache-miss advantages.
- Symmetry 14y agoDoes dynamic compilation really that much better than profile guided optimization? That is, I suspect that in the situation you describe a C++ compiler using PGO would do just as well at detecting which of the implementations are present. In theory this might change between runs, but I can't believe that's as common as it being the same.
- pron 14y agoI suspect that in the situation you describe a C++ compiler using PGO would do just as well Quite possibly, though not necessarily better. And you still won't have the memory management advantages. But let me revise my statement: if you have unconstrained development resources, you could probably make large C++ software a little faster than a Java counterpart in many cases, but that little improvement would come at a very big development cost in almost all cases.
- Symmetry 14y agoIn cases where developers have unconstrained resources I'd actually expect C{,++} to be about 50% faster than Java as you see in the programming languages shootout due to better ability to optimize memory use. But I'd expect most professionally produced software to use PGO, and hence get most of the same benefit from runtime information as Java.
- pron 14y agoBut you can't optimize large software that has to be maintained. Even if you could (because you have unlimited resources), you won't be getting a 50% improvement because you'll be spending a lot of the time on IO and task switching anyway. And just to make sure: whatever improvement you get will be very costly. We're talking manual control of memory fences, NUMA memory allocation and placement, lock biasing and more. All stuff the JVM does a very good with little effort for the Java programmer.
- groby_b 14y agoI just fell off my chair laughing. 50KLOC and 5 people is _tiny_. C++ developers work at projects about 100 times that size and still beat the performance of Java easily. (Linux comes to mind. Firefox, Safari, Chrome. The various JITs are smaller, but still well beyond 50KLOC. Clang. Python.) Sorry, Java has many advantages. Speed isn't one of them.
- codewright 14y ago>Sorry, Java has many advantages. Speed isn't one of them. This is part of my problem, very few who don't have to confront the challenges or disadvantages of their technology of choice seem willing to acknowledge them. That is a dire problem in what is ostensibly an engineering discipline. We don't get to say concrete has higher tensile strength than steel just because we think concrete is the best thing ever. That programmers can't even agree on basic notions of what's true does a lot to discredit our field.
- splicer 14y ago> Linux comes to mind Linux is written in C, not C++.
- pron 14y agoWeb browsers are not the best fit for Java because they require short startup times and must do their best not to waste too much RAM. Linux is an OS, requires predictability, and also must not waste RAM. Compilers certainly require fast startup, and might not even run long enough for the JVM optimizations to kick in. Java has many advantages, and speed is chief among them, but memory footprint certainly isn't. And I'm not sure how you can say that the software you mention beats Java (let alone easily). You have nothing to compare it to. Those programs' requirements preclude Java before any speed considerations are taken. If you can't spare lots of RAM, and don't have the time to wait for the JIT to profile and compile your code, Java isn't for you. But if you just want speed for an always-online application - especially if it requires serious concurrency - I'd say Java would be a better choice than C++, performance-wise, 9 times out of 10. If I were to pick an example to try and disprove my statement, I would actually choose Oracle or MySQL. But I don't know what requirements make them a better fit for C/C++ if, in fact, they are at all.
- Shorel 14y agoYeah, that's why the game industry uses Java almost exclusively. NOT!