why Java Math slower than C Math?

Tom Tromey tromey@redhat.com
Tue Feb 25 18:55:00 GMT 2003


>>>>> "Manfred" == Manfred Bergmann <bergmannmd@web.de> writes:

Manfred> I am doing some performance tests between C/C++ and Java.  An
Manfred> algorithm performs on a lot of float elements in an array
Manfred> where quite often math-functions (abs, min, max) are called.

First, java does bounds checking of array accesses.  gcj curently
doesn't try to eliminate bounds checks, so this adds overhead.
You can compile with -fno-bounds-check to disable this, though in that
case a bounds violation will cause unpredictable behavior.

Manfred> On the same code-base, Java performs unoptimized slower than C.
Manfred> With -O3 optimization both have nearly the same performance.
Manfred> AFAIK, all java.lang.Math methods are declared static, does this
Manfred> mean, that they are made to inline-funktions with a -O3 optimization
Manfred> and with that Java is as fast as C?

A few methods in java.lang.Math are treated as builtins at -O1 and
greater.  Currently these are min, max, abs, cos, sin, and sqrt.  We
might be able to add more, but I haven't really kept up with that
area.

Tom



More information about the Java mailing list