Reputation: 35359
If C#, Java, or C++, for example, all compile to machine code, why are they not equally performant?
My understanding is that such languages are an abstraction of machine code, which is what they all eventually compile to. Shouldn't the processor determine performance?
Upvotes: 12
Views: 907
Reputation:
Consider the differences in the languages and the overhead this occurs -- even if such additional work is done "at the same efficiency" there is just more to do. Period. (This is the price that often comes with abstraction: development time can be [substantially] decreased for [moderate] increases in run-time.)
On the other hand, with a trivial function using no "language features" such as a loop computing a factorial ... then the numbers can become very competitive in certain cases. This can be seen in The Computer Language Benchmark Game (here is Java7 vs. C++, for instance).
Note that the implementation of a language (including JIT) and the optimizations ("-Ox") applied is also a major factor. (A language arguably "has no speed" in and of itself.)
Happy coding.
As Been Voigt pointed out, the JIT/AOT models are optimized for different aspects. (The Sun Oracle Java implementation even has a separate server VM and client VM which each prioritize difference use-cases.) Here are some SO posts discussing JIT vs. AOT:
Upvotes: 6
Reputation: 2692
If C#, Java, or C++, for example, all eventually compile to machine code, why are they not equally performant?
Most simply - they don't all compile to the same machine code.
On a slightly different note, please understand that a lot of the claims about performance you'll see on the web will be wrong, and a lot of the measurements of performance you'll see on the web are out-of-date or unreliable or broken in some other way.
For example, phresnel pointed to measurements of a tiny multiplication program, and those measurements were:
made with Java 1.4 back in 2003 - the current version is Java 7
made in a very naïve way which prevented Java from completing compilation
Let's just run his program half-a-dozen times without re-starting the JVM and see what happens:
public class mult {
public static void main(String[] args){
for (int i=0; i<6; ++i) mult.program_main(args);
}
public static void program_main(String[] args) {
long nbStep = 1000000000;
long tCPU = System.currentTimeMillis();
double t=1. , r= 0.9999999999999999999999999999999999;
if ( args.length > 0 ) {
nbStep = Integer.parseInt(args[0]);
System.out.println( args[0] + " demandees" );
}
for ( long i = 0; i < nbStep; i++ ) {
t = t * r;
}
tCPU = - tCPU + System.currentTimeMillis();
System.out.println(nbStep + " multiplications en " +
tCPU + " millisecondes ." );
}
}
$ /usr/local/src/jdk1.7.0/bin/java -XX:+PrintCompilation -XX:+PrintGC mult
53 1 % mult::program_main @ 57 (122 bytes)
4662 1 % mult::program_main @ -2 (122 bytes) made not entrant
1000000000 multiplications en 4609 millisecondes .
4662 1 mult::program_main (122 bytes)
4669 2 % mult::program_main @ 57 (122 bytes)
1000000000 multiplications en 4612 millisecondes .
1000000000 multiplications en 564 millisecondes .
1000000000 multiplications en 563 millisecondes .
1000000000 multiplications en 563 millisecondes .
1000000000 multiplications en 563 millisecondes .
Once Java completes compilation the times drop from 4609ms to 563ms.
The Java code is 8 times faster than the naïve measurement would have you believe.
Upvotes: 2
Reputation: 71586
Are you aware that the same C++ code does not produce the same machine code with different compilers or different versions of the same compiler? Some compilers will take the same source and create a binary for the same target that is significantly faster than another compiler. For the same reasons other languages that compile to machine code will not perform the same. Some languages are easier to compile/optimize than others. Languages like Java do not compare as they do not compile to machine code they normally compile to a system independent bytecode and then run on a jvm, a virtual machine. the jvm being some code in some language compiled by some compiler which can be fast or slow depending on the code and compiler chosen. interpreted languages like java (bytecode) are slow compared to compiled directly to machine code.
Take some time to learn how to disassemble binaries that you have compiled. Read up on the bytecode type instruction sets behind java, python, etc. p-code that pascal used to use, etc, etc.
if you are talking about x86 computers you have a vast performance difference across that family. You can compile a binary that runs very fast relative to the clock rate on one x86 processor but the same binary runs really slow on another, usually the newer processor with the faster clock rate runs an older binary slower. Within the x86 world it is a futile effort to create a single binary that runs fast everywhere, so your compiler, if performance is desired, has to work significantly harder to try to target performance per system/processor.
Your question is similar to asking, if all vehicles basically have an engine and four wheels why can some go faster? Why can some haul more stuff than others?
Upvotes: 3
Reputation: 283883
For one thing, C++ optimizers are much more mature. Another, performance has always been the overarching goal of the C++ language designers ("you don't pay for what you don't use" is the mantra, which clearly can't be said about Java's every-method-is-virtual policy).
Beyond that, C++ templates are far more optimization-friendly than Java or C# generics. Although JITs are often praised for their ability to optimize across module boundaries, generics stops this dead in its tracks. The CLR (.NET runtime) generates only one version of machine code for a generic which covers all reference types. On the other hand, the C++ optimizer runs for each combination of template parameters, and can inline dependent calls.
Next, with C# and Java you have very little control over memory layout. Parallel algorithms can suffer an order of magnitude performance degradation from false sharing of cache lines, and there's almost nothing that the developer can do about it. OTOH C++ provides tools to place objects at specific offsets relative to RAM pages and cache boundaries.
Upvotes: 9
Reputation: 26281
"If C#, Java, or C++, for example, all eventually compile to machine code, why are they not equally performant?"
Both C# and Java compile to a bytecode which is eventually reduced to machine code by a virtual machine (e.g. for Java its called JVM). C++, however, is generally compiled down to assembly level initially.
The virtual machines can actually perform certain optimizations at runtime (one common example is bimorphic inlining) but in other cases the added overhead adversely affects the performance
Upvotes: 5