views:

420

answers:

5

What language is faster? Java or C++? On web, I have come across a multitude of benchmarks who claim one is faster than the other.

And yes, I would also like to know whether these differences do have any significance in application development or not? I am really confused. Please shed some light on this. Thanks.

+4  A: 

Java is faster to develop in, and well written C++ will outperform Java.

Jesse Weigert
I don't agree on Java being faster to develop. Extremely strict type system, mandatory exception specifiers and having to use try-finally everywhere slow the development down considerably. Java's benefit is automatic garbage collection, but C++'s auto storage and STL containers (not using pointers at all) are even easier to use. For those cases where they are not sufficient, smart pointers or ptr_containers can be used. Only in the very rare cases involving shared ownership and cyclicity the GC wins.
Tronic
Tronic: C++ can also be garbage collected: http://www.hpl.hp.com/personal/Hans_Boehm/gc/
fullreset
+1 - throw in the word 'typically', as in '...is typically faster to develop in...', and I think your answer is perfectly correct.
Scott Smith
+3  A: 

In gneral, applications written in C++ are faster than the equivalent apps in Java because they're in native code, while Java is byte code interpreted by the JVM.

Bruno Rothgiesser
I'm sure SO will correct me if I'm wrong, but I don't believe that this is always necessarily true. The JIT compiler can greatly improve the speed of a Java application even over that of "native code." (Not to mention that the speed of an application really depends on the algorithms being used, anyway.)
JasCav
@Jason: in theory it could, in practice it never ever happens and JIT compiled code is slower than native code. And let's not talk about the overhead of GC, etc
Andreas Bonini
GC is often considered faster than self managed memory.
Pool
Jason: It is unlikely that a JIT compiler would produce better code than an offline optimizing compiler -- spending a second optimizing each long/complex sequence of code on first execution would not be considered 'acceptable' in terms of user experience. Furthermore, since JIT compilers tend to work on smaller units of code they tend to miss opportunities for inlining and other optimizations only possible given a larger view of the program logic.
fullreset
The bigger issue (to me, at least) is how typical is it for developers to write correct code in java vs. C++. If development speed takes into account months of bug fixing because the outsourced developers didn't really understand resource management in C++...
Scott Smith
+12  A: 

Languages don't have a speed.

David Crawshaw
+1. Compilers can have a speed, languages don't.
T.E.D.
+1  A: 

I asked similar questions with regards to numerical computing few months ago. consensus seemed to be that Java is slower than decent C++ compiler. it seems Java just-in-time compiler is not able to vectorize/simdize loops. so in that regard Java is slow.

aaa