share
Stack OverflowWhat is faster : Java or C++?
[-1] [5] Yuvraj Sankad
[2010-02-25 18:21:39]
[ java c++ performance ]
[ http://stackoverflow.com/questions/2336526] [DELETED]

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.

A bad algorithm in C++ will be slower than a good algorithm in Java. What are you asking? - S.Lott
(11) Note this isn't the "real" GMan. - community_owned
Depends on what your goal as a programmer is! - Secko
Is it mean anything for web projects? Did you know any web project where site code was a bottleneck, not DB layer? - splix
The only way to know is to measure. Write the code in both, then measure. Measure. - jeffamaphone
(1) @Neil, ah wait. He really got me. I thought he is! Ahaha - Johannes Schaub - litb
(1) @Neil, what do you mean by "he isn't the real GMan" ? Is there someone else with this name here? - Yuvraj Sankad
C++ runs about twice as fast as Java. There are some tests where Java can run faster and then there are tests where C++ can run much faster, but 2x is the best average that I know. - Tronic
(1) @S.Lott - the fact that you can construct unfair comparisons doesn't change the fact that you can construct fair comparisons too. I'm not claiming you can make a fair comparison, just finding your point irrelevant. - Steve314
(2) @GMan stackoverflow.com/users/87234/gman - community_owned
[+12] [2010-02-25 18:25:44] David Crawshaw

Languages don't have a speed.


+1. Compilers can have a speed, languages don't. - T.E.D.
1
[+5] [2010-02-25 18:24:42] Jesse Weigert

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


(5) 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: 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
2
[+3] [2010-02-25 18:25:05] Bruno Rothgiesser

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.


(7) 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
(2) @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
(4) GC is often considered faster than self managed memory. - Pool
(1) 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
(1) 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
3
[+2] [2010-02-25 18:27:05] N 1.1
4
[+1] [2010-02-25 18:27:24] Anycorn

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.


5