According to Julie said:
I am going to write a program cracking DES.
One says that C++ (actually C) is faster than Java.
Is this true?
Usually, Java is compiled into bytecode, which is somehow native code
for a "virtual machine". When the program is executed, the bytecode is
compiled into native code (by the "JIT" compiler). That compilation
handles only local optimizations, and is not good as other types of
optimizations (such as inlining). Besides, Java is designed so as to be
"safe", therefore all array accesses are checked with regards to the
array length. This slows down Java much. On pure integer computations
(such as DES cracking), you may expect a factor of 3 between a Java
implementation and an optimized C (or C++) implementation.
Of course, since most applications are I/O constrained, or limited by
the memory bandwidth, that slowdown factor is of little importance,
except for the very few routines which require raw cpu power. A DES
cracker is a very uncommon application.
Besides, you can get much worse performance, both with Java and C, if
you do not know what happens "under the hood". For instance, using
dynamic memory allocation (new, malloc()...) inside the inner loop can
kill speed very effectively. If you want to produce optimized code,
you have to take care of the fine details.
As for C vs C++, this is a long debated issue. The C++ model can put
some limitations on the compiler (for instance, exceptions have to
work out their way through the stack frames, which means that the
compiler cannot become too fancy with them) but it can also provide the
programmer with syntaxic constructions which help in building optimized
code. A very good C programmer and a very good C++ programmer, both
using proper tools, will produce programs of similar performance. On
some architecture (such as the PC), a very good assembly programmer will
beat both (at the cost of much reduced portability).
--Thomas Pornin