-
Enhancement
-
Resolution: Cannot Reproduce
-
P4
-
None
-
2.0
-
x86
-
windows_2000
Name: ns13034 Date: 07/18/2000
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)
There seems to be a problem with the floating point code generation in Hotspot
on Intel. The following code demonstrates the timing differences between
various versions of the same algorithm.
public class MathTest {
public MathTest () {
go(new OriginalCalculation());
go(new StrictOriginalCalculation());
go(new NativeOriginalCalculation());
go(new HackedCalculation());
go(new NativeHackedCalculation());
go(new NoDivisionHackedCalculation());
go(new StrangeDivisionHackedCalculation());
}
public void go(Calculation c) {
long ctime = System.currentTimeMillis();
for (int y=0; y < 533; y++) {
for (int x=0; x < 800; x++) {
c.calculation(x, y);
}
}
System.out.println(c.getClass().getName() + " " +
(System.currentTimeMillis() - ctime));
}
/**
* Main method
*/
public static void main(String [] args) {
MathTest t = new MathTest();
}
interface Calculation {
void calculation(double x, double y);
}
/* All timings are from a Pentium 3 550Mhz and are in mSec */
/**
* Performance: BAD (~1730)
* This is the original calculation that doesnt perform well.
*/
class OriginalCalculation implements Calculation {
public void calculation(double x, double y) {
double radius = Math.sqrt(x * x + y * y);
double theta = Math.acos( x / radius);
}
}
/**
* Performance: BAD (~1760)
* Original calculation with strict floating point switched on
*/
class StrictOriginalCalculation implements Calculation {
public strictfp void calculation(double x, double y) {
double radius = Math.sqrt(x * x + y * y);
double theta = Math.acos( x / radius);
}
}
/**
* Performance: GOOD (~420)
* instead of calling Math routines call a native method which simply
calls
* 'C' math library function and returns
*/
class NativeOriginalCalculation implements Calculation {
public void calculation(double x, double y) {
double radius = MyMath.sqrt(x * x + y * y);
double theta = MyMath.acos( x / radius);
}
}
/**
* Performance: BAD (~1740)
* Split calculation into pieces
*/
class HackedCalculation implements Calculation {
public void calculation(double x, double y) {
double x2 = x * x;
double y2 = y * y;
double radius = Math.sqrt(x2 + y2);
double ah = x / radius;
double angle = Math.acos(ah);
}
}
/**
* Performance: GOOD (~440)
* Isolate to see if calling math lib is the problem
*/
class NativeHackedCalculation implements Calculation {
public void calculation(double x, double y) {
double x2 = x * x;
double y2 = y * y;
double radius = MyMath.sqrt(x2 + y2);
double ah = x / radius;
double angle = MyMath.acos(ah);
}
}
/**
* Performance: GOOD (~400)
* Ok. So what gives. I am still using the Math library but if i get
rid of the
* division everything speeds up...
*/
class NoDivisionHackedCalculation implements Calculation {
public void calculation(double x, double y) {
double x2 = x * x;
double y2 = y * y;
double radius = Math.sqrt(x2 + y2);
double ah = x; // COMMENTED OUT. / radius;
double angle = Math.acos(ah);
}
}
class StrangeDivisionHackedCalculation implements Calculation {
public void calculation(double x, double y) {
double x2 = x * x;
double y2 = y * y;
double radius = Math.sqrt(x2 + y2);
double ah = x2 / radius;
double angle = Math.acos(ah);
}
}
}
(Review ID: 105926)
======================================================================
- relates to
-
JDK-4461243 RFE: Math.acos very slow
-
- Closed
-