why java takes more time to execute than c/c++

i m more comfortable doing coding in java.But all my executions in java is taking more time comparision to c/c++ done by others.

This is because

In pure code execution speed, Java is about equal to straightforward C++. But Java tends to use a lot more memory - partially because it’s GC-based, partially because its design places more focus on simplicity and safety than on efficiency. Due to cache issues, more memory translates to lower speed. A lot lower when compared to highly-tuned C++.

And the main reason is Java runtime isnt interpreting bytecode. Rather, it uses whats called Just In Time Compilation. Basically, as the program is run, it takes bytecode and converts it into native code optimized for the particular CPU.


1 Like

JIt improves the execution speed of the Java program, but doesn’t your answer above says that it is taking more time because of JIT? May be you are correct, Because of the checks like Nullpointer, array bounds, garbage collection and mainly the fact that, C/c++ code is directly converted into machine code and java needs JVM for executing its bytecode, it takes more time.

But Can you please explain, how actually everything happens while executing a java program with side comparison in C/C++ and how at what stage JIT does what. This may help: http://programmers.stackexchange.com/questions/159373/what-backs-up-the-claim-that-c-can-be-faster-than-a-jvm-or-clr-with-jit A good link but things are not much explained deeply in flow. Thanks for the help…

hey bro i have searched for this answer over internet and on a discussion site i found this answer with many upvotes!!

Although while searching i found that it is hard to say that java is always slower than c++ or c because many claims that this is a pointless topic for discussion and both languages have pros and cons like you may read about the pros and cons here http://www.azulsystems.com/blog/cliff/2009-09-06-java-vs-c-performanceagain.