Java - Faster alternative to Math.pow() and Math.sqrt()

16,425

Solution 1

You are unlikely to find a better(faster) implementation than the Java Math one. You might have more luck trying to change the way you do the calculations in your algorithm. For example, is there any way you can avoid finding the square root of a huge number?

If this doesn't work, you can try implementing it in a more appropriate language that's meant for fast mathematical computations (something like Matlab).

Otherwise, you can try to optimize this in other areas. Perhaps you can try to cache past results if they are useful later.

Solution 2

"The power of 2" is squaring. You'd be better off doing that by multiplying the number by itself.

The library version of sqrt is probably faster than anything you could dig up elsewhere. If you call a C routine, you'll just add overhead from the cross-language call. But do you need accurate square roots, or would a table lookup of approximations do? Do the values repeat a lot, i.e. do you often need to calculate the roots of the same numbers? If so, caching the square roots in a HashMap might be faster than computing them.

Solution 3

the only thing I can think of is storing the results for speed, the square root will not change, and ~9000 stored numbers is not that many. You would probably do well to frame your data, so that you could ensure that you can optimally search for the appropriate result.

Solution 4

Well your problem with the power of 2 can simply be done by multiplying the number by itself. For example, lets say variable a is the number you want to raise to 2. It's the same as: int a=5; int b=a*a;

Share:
16,425
M9A
Author by

M9A

Updated on June 14, 2022

Comments

  • M9A
    M9A almost 2 years

    My program uses Math.pow() to compute a relatively large double number to the power of 2. Later on I need to find the square root of a very large double number. The problem is, I have to do this over a 100,000 times and it is taking really long. Is there any alternative that can speed this process up? Thanks

    Edit: By large numbers I mean between 1000 to 10000 (So probably not that large in computing terms). And in terms of it taking a long time, it takes about 30 secs to do the function 500 times

  • M9A
    M9A about 11 years
    Well it involves the Euclidean distance formula so I cant avoid it
  • John Dvorak
    John Dvorak about 11 years
    @Matt9Atkins do you really need the euclidean distance? You could use its square for comparisons...
  • Oleksi
    Oleksi about 11 years
    Well there are still options for speeding this up. Do you need the actual distance, or are you just comparing them? If you're just comparing them, you can avoid the square root part.
  • John Dvorak
    John Dvorak about 11 years
    @Oleksi I guess what the other answer suggests is a speed improvement over Math.pow(x, 2)
  • M9A
    M9A about 11 years
    @Oleksi - I will need the actual computed distances as I will need to compare them at the end
  • Oleksi
    Oleksi about 11 years
    If you need to compare them, then will get the same result if you leave the distances in the not square root form! Note that if x < y, then sqrt(x) < sqrt(y) and vise versa. This can really speed up your program!
  • NovaDenizen
    NovaDenizen about 11 years
    Surely you mean you can use x*x instead of pow(x,2). And yes, it is certainly better to calculate it as x*x.
  • Tuupertunut
    Tuupertunut over 6 years
    Have you tried Math.hypot(x, y) which is designed specifically to solve the Euclidean distance?