Name: yyT116575 Date: 05/21/2001
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Server VM (build 1.3.1-b24, mixed mode)
There is a sample code computing an estimation of orthodromic distance between
two geographic points. Note that this computation involve 8 sin/cos/sqrt
operations and only one acos operation.
public class Ellipsoid {
private double a = 6378137.0;
private double b = 6378137.0*(1-1/298.257223563); // WGS 1984
public double distance(double x1, double y1, double x2, double y2) {
y1 = Math.toRadians(y1);
y2 = Math.toRadians(y2);
final double y = 0.5*(y1+y2);
final double dx = Math.toRadians(Math.abs(x2-x1) % 360);
final double ang = Math.sin(y1)*Math.sin(y2)
+ Math.cos(y1)*Math.cos(y2)*Math.cos(dx);
final double ier = hypot(Math.sin(y)/a , Math.cos(y)/b);
return Math.acos(ang)/ier;
}
private static double hypot(double x, double y) {
return Math.sqrt(x*x + y*y);
}
public static void main(String[] args) {
double sum=0;
final Ellipsoid ell=new Ellipsoid();
for (int i=0; i<20000000; i++) {
double x1 = Math.random()*10+20; // 20 to 30?E
double x2 = Math.random()*10+20; // 20 to 30?E
double y1 = Math.random()*10+40; // 40 to 50?N
double y2 = Math.random()*10+40; // 40 to 50?N
double dx = ell.distance(x1,y1 , x2,y2);
sum += dx;
}
System.out.println(sum/20000000);
}
}
There is a portion of the profiling output (code was run with -server and
-Xprof options).
Compiled + native Method
8.9% 2746 + 0 Ellipsoid.main
6.6% 2051 + 0 Ellipsoid.distance
0.0% 3 + 0 adapters
15.6% 4800 + 0 Total compiled
Stub + native Method
36.0% 81 + 11032 java.lang.StrictMath.acos
36.0% 81 + 11032 Total stub
Thread-local ticks:
0.0% 5 Class loader
48.1% 14839 Unknown: running frame
0.1% 21 Unknown: thread_state
The JVM spend 36% of its time into the "acos" function, which is only about 12%
of all trigonometric functions. In my real program (which involve I/O,
searching, sorting and distances computation), this only "acos" call eat 32% of
my CPU time. Since my computation take a few hours, I would appreciate hardware
acceleration if it is possible.
(Review ID: 124713)
======================================================================
- relates to
-
JDK-4354142 Floating point performance problem
-
- Closed
-