c++gccoptimization

C++ compiler optimization with shorthand IF


I have this question in mind regarding optimazation of compiler when it comes to shorthanded if/else.

I have this function:

double eu_distance (const coor& x, const coor& y) {

return ((y.x - x.x)*(y.x - x.x) + (y.y - x.y)*(y.y - x.y));
}

I am wondering what is more efficient?

min = min > eucl_distance(point_a, point_b) ? eucl_distance(point_a, point_b) : min;

or

double dis = eucl_distance(point_a, point_b);
if (min > dis)
    min = dis;

in the former case, does compiler (in my case, GCC 4.6.2) know how to optimize that if/else to keep the return value of eucl_distance() to reuse instead of computing it twice?

A piggy back question would be:

What is more efficient?

(y.x - x.x)*(y.x - x.x)

or

pow((y.x - x.x),2)

PS: Sorry that I cannot pick more than one correct answers!! :( Thank you all for your answers! I really appreciate them!!


Solution

  • There's no universal answer: you'll have to profile the code generated by your implementation to know for sure. In most cases, however, if eu_distance is in a separate translation unit, and is not specially annotated, the compiler will be unable to know that calling it twice with the same arguments will give the same results; in this case, the second form will almost surely be faster. On the other hand, If eu_distance can be inlined, any decent optimizer will end up generating almost exactly the same code for both.

    In practice, I would almost certainly use a third form:

    min = std::min( eu_distance( point_a, point_b ), min );
    

    (I am supposing that eucl_distance is a typo for eu_distance.)

    Also, I'd avoid a name like min. Somebody's too likely to add a using namespace std; later, or even to include <windows.h>, without hvaing defined NOMINMAX. (<windows.h> defines min and max as macros if NOMINMAX has not been defined. Which leads to some interesting error messages if you define your own min or max. Or even include <algorithm>.)

    Concerning pow( x, 2 ): again, you'll really have to measure, but typically, x * x will be faster, even if x is a complicated expression. (Of course, if the expression is non trivial, then recognizing that both x are identical may not be that easy, which makes the code harder to read. In such cases, you might want to consider a small function, say squared, which does nothing but return x * x. Inline it if it makes a difference in performance.)