HomerCritic said:
Ok, 1.95 seconds in C vs 3.3 seconds in Java.
This is a very loose comparison ofcourse, but one could (ignorantly)
conclude that Java must be roughly 50% slower than C. This is much
less bad than many C die-hards and/or anti Java people think by the
way.
In your example you are comparing access via a pointer with access via
an index. There is a good chance that the pointer access and increment
can be compiled into very few assembler statements. If you have a
processor which has an assembler instruction for incrementing a
register, not much is needed for the pointer arithmetic, and a good C
compiler can reduce the loop body to almost nothing.
Compared to the index access in Java, where - in the non optimized case
- you need some real arithmetic to calculate the address of the element,
and where you probably have got to go through an additional indirection
level just to get the base of the array (dereference the object), the
Java result is almost to good
In the end, ultimately, logic for logic, Java is most of the time a
little slower, sometimes faster. But not nearly as astronomically much
slower as many stuborn C diehards continue to think. With Java around,
companies would be nuts to invest in C/C++ development these days,
unless there is a performance case for it.
This reminds me of a "benchmark" in an otherwise good German computer
magazine. One of their regular writers (a die-hard Delphi and
<whatever>.NET programmer) was running a home-grown toy "benchmark". It
was a collection of meaningless small programs. I had the impression
that he tried very hard to let his favorite languages look good, but at
the end he had to admit that the Java results weren't to bad. His C/C++
stuff was in general slower, only his beloved Delphi stuff was most of
the times faster - surprise, surprise
You can supplement Java with C/C++ and Assembler even, for that oddball
compression routine, or graphic processing routines and such.
You don't need to do that for graphics, unless you need the last bit of
performance. More and more of the Java2D graphics system use the
underlying OS graphics system via native methods to do things.
/Thomas