Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is more efficient, atan2 or sqrt?

There are some situations where there are multiple methods to calculate the same value.

Right now I am coming up with an algorithm to "expand" a 2D convex polygon. To do this I want to find which direction to perturb each vertex. In order to produce a result which expands the polygon with a "skin" of the same thickness all around, the amount to perturb in that direction also depends on the angle at the vertex. But right now I'm just worried about the direction.

One way is to use atan2: Let B be my vertex, A is the previous vertex, and C is the next vertex. My direction is the "angular average" of angle(B-A) and angle(B-C).

Another way involves sqrt: unit(B-A)+unit(B-C) where unit(X) is X/length(X) yields a vector with my direction.

I'm leaning towards method number 2 because averaging angle values requires a bit of work. But I am basically choosing between two calls to atan2 and two calls to sqrt. Which is generally faster? What about if I was doing this in a shader program?

I'm not trying to optimize my program per se, I'd like to know how these functions are generally implemented (e.g. in the standard c libraries) so I'll be able to know, in general, what is the better choice.

From what I know, both sqrt and trig functions require an iterative method to arrive at an answer. This is the reason why we try to avoid them when possible. People have come up with "approximate" functions which use lookup-tables and interpolation and such to try to produce faster results. I will of course never bother with these unless I find strong evidence of bottlenecking in my code due to just these routines or routines heavily involving them, but the differences between sqrt, trig funcs, and inverse trig funcs may be relevant for the sake of discussion.

like image 265
Steven Lu Avatar asked Feb 16 '12 18:02

Steven Lu


2 Answers

With typical libraries on common modern hardware, sqrt is faster than atan2. Cases where atan2 is faster may exist, but they are few and far between.

Recent x86 implementations actually have a fairly efficient sqrt instruction, and on that hardware the difference can be quite dramatic. The Intel Optimization Manual quotes a single-precision square root as 14 cycles on Sandybridge, and a double-precision square root as 22 cycles. With a good math library atan2 timings are commonly in the neighborhood of 100 cycles or more.

like image 179
Stephen Canon Avatar answered Sep 27 '22 20:09

Stephen Canon


It sounds like you have all the information you need to profile and find out for yourself.

If you aren't looking for an exact result, and don't mind the additional logic required to make it work, you can use specialized operations such as RSQRTSS, RSQRTPS, which calculate 1/sqrt, to combine the two expensive operations.

like image 33
Guvante Avatar answered Sep 27 '22 20:09

Guvante