i'm kinda a noob at programming but i was wondering if you write two of the same programs in two different compiled languages (i.e. java and c++) and run them after you compile them, does the runtime differ or is it only the compilation time that differs?
-
1It's misleading to say that C++ and Java are 'both compiled languages' because C++ compiles to machine code and Java compiles to virtual machine bytecode, which inherently has a huge affect on performance. – 0x5f3759df Dec 10 '11 at 23:48
-
2Languages do not have speed. Implementations do. This question is primarly "JIT vs compiled" and has been asked many times before. – Dec 10 '11 at 23:48
-
6To get you started ... http://stackoverflow.com/questions/7591169/why-is-it-hard-to-beat-aot-compiler-with-a-jit-compiler-in-terms-of-app-perfor , http://stackoverflow.com/questions/7448508/java-vs-c-raytracing , http://stackoverflow.com/questions/1550910/c-and-java-performance , http://stackoverflow.com/questions/1517868/performance-of-java-1-6-vs-c , http://stackoverflow.com/questions/313446/can-anyone-quantify-performance-differences-between-c-and-java , http://stackoverflow.com/questions/145110/c-performance-vs-java-c – Dec 10 '11 at 23:51
2 Answers
The rule of thumb is interpreted (compiled on the fly) is slower than pre-compiled, compiled to virtual machine like Java is slower than architecture dependant, and procedural is faster than OO and assembler faster than procedural, (but you don't wanna use that unless you're programming something like a PIC microcontroller for an industrial application or you're some kind of mixture between Neo and a programming Jedi)
Anyway, each language has some advantages and drawbacks, and also a good optimization (indentifying the bottlenecks and improving performance on those particular points) can make your program run faster than any other careless implementation no matter what language...

- 8,344
- 5
- 40
- 70
-
1However, that's rule of thumb with a capital thumb (i.e. incredibly approximate and overgeneralizing). For instance, JIT-compilers (such as HotSpot) can optimize more aggresively (inlining indirect/`virtual` calls and across dynamically-loaded libraries) and today's mainstream C compilers can outsmart most humans 99% of the time. – Dec 11 '11 at 00:22
-
You're right about compilers constantly improving but still I don't know: if you want to make sure things are done right you gotta make them yourself. Most compilers optimize more than aceptably for huge applications, but I don't see myself using gcc to compile a program for a led or sensor microcontroller or even a basic industrial robot arm, though my friend at the technical school told me they're teaching that now... – NotGaeL Dec 11 '11 at 09:06
-
@delnan: Has anyone measured Java to be faster than C on a non-trivial application? (Not cheating by using a toy C compiler or anything.) I ask because I hear this aggressive optimization argument a lot, but I've never seen it pan out in the real world. But then again, I've seen PHP outperform Java, probably because the Java code wasn't exactly optimal (read: could be DoS'd by a single client repeatedly clicking the mouse to toggle a setting). There's a lesson buried in there somewhere… – Left For Archive Aug 06 '12 at 14:58
-
@LnxPrgr3 I don't follow the Java ecosystem much, but I know that PyPy's JIT can generate far more specialized code based on runtime values (e.g. generate two str() calls - which may also be type-specialized - and low-level string manipulation for `"<{0},{1}>".format(point)` where the format string came from a file; cf. http://morepypy.blogspot.de/2011/08/pypy-is-faster-than-c-again-string.html) but "non-trivial applications" are much harder to benchmark. And yes, there's a lesson in there - a "fast language" is just a lower bound, you can do arbitrarily bad no matter how good you *could* do. – Aug 06 '12 at 15:27
Java code is slower than C++. As with any studies, the numbers can vary quite a bit. You usually see 20% slower than C/C++ as the most in favor of Java. Wikipedia compares them in some more detail.
Other sources say it uses 50% more memory.
In short: Java is slower than C, C++ and other languages that compile to machine code (versus Java's bytecode).

- 84,529
- 20
- 165
- 173
-
-
Just broadly stating "Java code is slower than C++" is bound to be challenged, so I'll play devil's advocate... Have a look at http://en.wikipedia.org/wiki/Java_performance, and either scrap the reference to Java bytecode or mention the JIT compiler. – Dec 11 '11 at 00:27
-
You can feel free to edit my answer, but I don't see how it could be faster. The Wikipedia page you linked to says, "the compiler cannot fully optimize the program, and therefore the resulting program is slower than native code alternatives." in the context of JIT. (I might be using the wrong terms in my answer, like bytecode. It would be nice if you can change them.) – Brigand Dec 11 '11 at 00:39
-
It can't be faster if the JIT produces code equivalent to what the C++ compiler produces. But it can produce better code! See http://en.wikipedia.org/wiki/Java_performance#Program_speed (includes references). Another example is http://morepypy.blogspot.com/2011/08/pypy-is-faster-than-c-again-string.html which isn't even Java but an "interpreted language" (Python), but it manages to inline and unroll library calls, something `gcc` can't do by design. – Dec 11 '11 at 00:46