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-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/gherardi1...
It would be interesting to get more recent benchmarks with the latest JVMs and C++ compilers.
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.
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[].
You kind of have them in Java 8, but not as primitive type.
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.
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).
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.
one in c++, one in java
10x is giving it too much credit.
Java wouldn't work very well!
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.
Powershell is a terrible example for the performance point I am trying to make, whatever about its flexibility.
[0] http://www.javalobby.org/java/forums/t72620.html
[0] http://stackoverflow.com/a/13496610
[1]http://www.ibm.com/developerworks/library/j-multitenant-java... (this now looks less experimental but I'm willing to bet >1 instance of eclipse, or bash for that matter would not work very well).
I'm not sure how this works: http://www.excelsiorjet.com/
but it might help to alleviate the problem even though it still has a jvm.
https://en.wikipedia.org/wiki/Java_Classloader
looking at what you mention now, can a program classload itself? seems not: A class with a given name can only be loaded once by a given classloader.
I doubt you would even have to go as far as native extensions for GUI before you start running into problems even though the programs are written in managed code.
Can you better explain how in practice Java programs could share a VM in replacing a typical Unix bash environment/userland?
Would they have to use the special IBM JVM? Would bash have to contain grep as a class? e.g.
machine:~$ grep include Source | grep -v 32 | grep -v 16
Reading about "JAR hell" I really don't think it would work very well.
Interestingly in the IBM link provided earlier they load up substantial non GUI servers such as Tomcat, Jetty and JRuby and achieve a startup time that is twice as fast.
They also have hello world:
Hello World Print "HelloWorld" and then sleep
Multi-tenant JVM: 309
Hand-tuned: 73
Default: 63
Improvement with multitenant: 4.2X to 4.9X
Even with this I think a C++ version would eat it for breakfast. It would be interesting to find out what would happen if a Single JVM were loaded on boot, all programs were loaded into that and compare the JVM based Unix on those terms with e.g. Solaris or something.
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.