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.

:slight_smile:

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: java - What backs up the claim that C++ can be faster than a JVM or CLR with JIT? - Software Engineering Stack Exchange 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.