开发者

Java performance problem for math.abs

I am using Jama matrix to perform SVD operations. I have couple of questions regarding performance.

I am not worried about so much accuracy, and I think double is more accurate than Float, am I right? If I use float other than double, how much will it improve the performance and reduce accuracy?

In Jama matrix it uses one function that it calls a lot, it used double and Math.abs function, that requires a lot of Heap and CPU. If I change it to double and remove Math.abs, how much will it impact the performance and results in terms of accuracy?

Here is the Jama math function:

   public static double hypot(double a, double b) {
      double r;
      if (Math.abs(a) > Math.abs(b)) {
         r = b/a;
         r = Math.abs(a)*Math.sqrt(1+r*r);
      } else if (b != 0) {
         r = a/b;
         r = Math.abs(b)*Math.sqrt(1+r*r);
      } else {
         r = 0.0;
      }
      return r;
   }

Here is the what I am thinking to do with this function

   public static float hypot(float a, float b) {
      float r;
      if (a > b) {
         r = b/a;
         r = (float) (a*Math.sqrt(1+r*r));
      } else if (b != 0) {
         r = a/b;
         r = (float) (b*Math.sqrt(1+r*r));
      } else {
         r = 0;
      }
      return r;
   }

I do not know, if it is a good开发者_如何学编程 way to do it or not. Thanks


I would expect a good JIT to inline a Math.abs call to just a single instruction. If your code is running on an FPU (quite likely), using float will not gain you any speed because nearly all FPUs are 64-bit or better.

However, the reason that the algorithm is so unusual is that it prevents overflow when the magnitude of its operands are on the order of 10^150. If you are considering using float, your operands must not be of magnitude larger than around 10^38, meaning that the fastest algorithm will just be:

public static double hypot(double a, double b) {
    return Math.sqrt(a * a + b * b);
}


Your approach will not work for negative arguments. Just add:

if (a < 0) a = -a;
if (b < 0) b = -b;

at the start of your function and all should be well. (By "not work" I mean that it will not always do the calculation in a way that minimizes rounding errors and risk of overflow.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜