4 ms·
At runtime modern Java is around 10x slower than modern C++ (assuming both are fully optimised and the benchmark is relatively unbiased). That isn't significan
by Someone1234 12y ago
At runtime modern Java is around 10x slower than modern C++ (assuming both are fully optimised and the benchmark is relatively unbiased).
That isn't significant on modern hardware relative to other bottlenecks (network, IO, etc). Plus people keep adding additional security layers between C/C++ and the CPU which eat away at some of its advantages (e.g. Docker containers, virtual machines, exploit detection libraries, etc).
Java speed matters in certain situations and for certain tasks. For example I wouldn't rewrite an SQL database into Java since performance could definitely be impactful there. But realistically CPU times are such a tiny minority of latency that it stopped being relevant a very long time ago.
- tshtf 12y ago[Citation required]
- Someone1234 12y agomike_hearn beat me to it. See his post for a citation (and some other numbers).
- afsina 12y agoAnd you stood corrected.
- mike_hearn 12y agoAt runtime modern Java is around 10x slower than modern C++ (assuming both are fully optimised and the benchmark is relatively unbiased). I don't think you can pick a single number like that, it depends very much on what the code is doing. You probably got this 10x figure from a Google paper published a few years ago (https://days2011.scala-lang.org/sites/days2011/files/ws3-1-Hundt.pdf https://days2011.scala-lang.org/sites/days2011/files/ws3-1-H...). Redux: the default out of the box 32 bit JVM (in 2011) was 12x slower than the same algorithm implemented in Java. But with a few simple GC flag changes, it became 3.7x slower. And then the Java version had a few further simple optimisations applied to the code and it became as fast as the original C++ version. Meanwhile the C++ version was optimised again and became around 3x to 5x faster, but that version relied heavily on Google proprietary data structure code and could not be open sourced (!). So for most programmers on most projects, it seems likely to be a wash. Meanwhile an alternative benchmark that reimplemented a non-trivial C++ program in Java found it became 1.09 to 1.5 times slower, but that was with Java 6 which is now two generations out of date: http://www.best-of-robotics.org/pages/publications/gherardi12java.pdf http://www.best-of-robotics.org/pages/publications/gherardi1... It would be interesting to get more recent benchmarks with the latest JVMs and C++ compilers.
- TheLoneWolfling 12y agoAs you say, it depends very much on what the code is doing. In particular, anything requiring manipulating unsigned values all over the place will be a lot slower. Also, see this[1] for talk about JGit. An actual project with direct comparisons. [1] http://marc.info/?l=git&m=124111702609723 http://marc.info/?l=git&m=124111702609723
- pjmlp 12y agoThose comparisons will be again out of date when Java 9+ comes out with value types, official unsafe package and lots of other goodies for performance being worked on.
- mike_hearn 12y agoI'm not aware of any intent on adding unsigned types in Java 9 though. The lack of them is a real PITA, for sure. Not only for performance but it's incredibly easy to forget a & 0xFF here or there and introduce bugs. The MMAP thing is also a good point. I believe HotSpot will compile the get methods on the MappedByteBuffer down to raw access so it shouldn't matter much for performance, in theory, but the code is still damned ugly. I never understood why they can't expose it as a byte[].
- pjmlp 12y ago> I'm not aware of any intent on adding unsigned types in Java 9 though. You kind of have them in Java 8, but not as primitive type. https://blogs.oracle.com/darcy/entry/unsigned_api https://blogs.oracle.com/darcy/entry/unsigned_api I also think a very least having ubyte would be quite nice. Apparently the reason behind the decision has to do with overflows and underflows in unsigned arithmetic. Hotspot is just one of many Java native compilers out there , so it is better not to rely on what Hotspot does. On the other hand, you can make use of JIT Watch or Solaris Studio debugging to see what Assembly is being generated. > I never understood why they can't expose it as a byte[]. Maybe the official unsafe class will make this better.
- 0x0 12y agoDo you have a source for that 10x number? I don't understand why a just-in-time java compiler would produce slower code than an ahead-of-time c++ compiler, especially since the java compiler has much more information available (exact cpu revision, cache and ram sizes, and hot spot profiling).
- mike_hearn 12y agoJava has certain qualities that make it sometimes harder to optimise, which is one reason it relies so heavily on profile guided optimisation. For example, it's very vulnerable to GC stalls if escape analysis can't identify all your variables as eligible for stack allocation. Escape analysis is complex and a little fragile, from what I've heard (I never tested it myself).
- pjmlp 12y ago> Escape analysis is complex and a little fragile, from what I've heard (I never tested it myself). Depends which JVM you are talking about. For example Graal is more aggressive than Hotspot in selecting variables for escape analysis. Each JVM (IBM, HP, Aonix, ...) has different quality levels in escape analysis.
- afsina 12y agoThat is totally wrong. I dealt with a lot of code from numerical computing to general data structures and I have never seen C++ code runs 10x faster than current JVMs. Heck not even with SIMD. From what I see for actual applications what you get is at best x1.5 speed up and a lot of agony on the C++ part.
- throwawayaway 12y agotime ./hello one in c++, one in java 10x is giving it too much credit.
- pluies_public 12y agoThat example will be horribly skewed by the JVM startup time, which is a known problem, but doesn't affect your program once it's started. It's pretty meaningless to compare programs that short, eg. scripts because noone sane would write them in either C++ or Java.
- throwawayaway 12y agoIf you were to reimplement Unix in Java: what would happen to the paradigm of small utilities that do one job well, that you combine into something greater than the sum of its parts? I think you'll find a lot of short programs are written in C++. Java wouldn't work very well!
- mike_hearn 12y agoUm, if you reimplemented UNIX in Java you'd probably do a much better job than the UNIX creators were able to do. In particular, running a program from bash would simply classload it into the existing VM, not invoke a whole separate VM, and then it'd be more or less instant, except you'd have the potential for much more flexible APIs and combinations of tools. Look at PowerShell for an example.
- throwawayaway 12y ago