1

When calculating the distance between two 3D points in Java, I can compute the distance, or the distance squared between them, avoiding a call to Math.sqrt.

Natively, I've read that sqrt is only a quarter of the speed of multiplication which makes the inconvenience of using the distance squared not worthwhile.

In Java, what is the absolute performance difference between multiplication and calculating a square root?

konsolas
  • 1,041
  • 11
  • 24
  • 2
    The only way to answer this "absolutely" is to benchmark the difference in your code. [Obligatory link](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Andy Turner May 30 '16 at 19:56
  • My tests show that `dx * dx + dy * dy` runs roughly twice as fast as `Math.sqrt(dx * dx + dy * dy)`. You mentioned *"quarter of the speed"*, meaning 4 times difference. If 2 or 4 times faster is of no concern to you, then it doesn't matter, does it? In the overall scheme of things, it only matters if you do it *a lot*. **Beware premature optimization.** Fix it if you measure a problem, don't convolute your code based on (faulty?) assumptions about performance. – Andreas May 30 '16 at 20:16
  • Computing a distance is taking the square root of the squared distance. There's no trade-off, the square root is pure overhead. However long it takes, is the time that computing a normal distance takes extra compared squared distance. – harold May 30 '16 at 20:18
  • You should consider what is a correct solution first, and then optimise it based on which code is taking the most time based on measurements rather than guessing e.g. using a profiler. Otherwise you can spend a lot of time optimising code which won't make any difference to your applications overall performance. – Peter Lawrey May 30 '16 at 20:47

1 Answers1

1

I Initially wanted to add this as a comment, but it started to get too bid, so here goes:

Try it yourself. Make a loop with 10.000 iterations where you simply calculate a*a + b*b and another separate loop where you calculate Math.sqrt(a*a + b*a). Time it and you'll know. Calculating a square root is an iterative process on its own where the digital (computer bits) square root converges closer to the real square root of the given number until it's sufficiently close (as soon as the difference between each iteration is less than some really small value). There are multiple algorithms out there beside the one the Math library uses and their speed depends on the input and how the algorithm is designed. Stick with Math.sqrt(...) in my opinion, can't go wrong and it's been tested by a LOT of people.

Although this can be done very fast for one square root, there's a definite observable time difference.

On a side note: I cannot think of a reason to calculate the square root more than once, usually at the end. If you want to know the distance between points, just use the squared value of that distance as a default and make comparisons/summations/subtractions or whatever you want based on that default.

PS: Provide more code if you want a more "practical" answer

Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29