Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why sqrt become much faster without -O2 in g++ on my computer?

Consider the following code:

#include <cstdio>
#include <cmath>

const int COUNT = 1000000000;

int main()
{
    double sum = 0;
    for (int i = 1; i <= COUNT; ++i) {
        sum += sqrt(i);
    }
    printf("%f\n", sum);
    return 0;
}

Without -O2, it runs only 2.9s on my computer, while it runs 6.4s with -O2.

My computer is Fedora 23, with g++ 5.3.1.

I have tried the same thing on Ubuntu 14.04 (with g++ 4.8), it doesn't have the problem (all 6.4s).

like image 722
debug18 Avatar asked May 05 '16 06:05

debug18


People also ask

How much faster is the square root algorithm?

The algorithm was approximately four times faster than computing the square root with another method and calculating the reciprocal via floating-point division.

Why is the inverse square root algorithm faster than floating point?

At the time, floating-point division was generally expensive compared to multiplication; the fast inverse square root algorithm bypassed the division step, giving it its performance advantage.

Why is Wi-Fi faster on the mobile than on the computer?

As you have seen, the causes of why Wi-Fi is faster on the mobile than on the computer usually have to do with a configuration error or an outdated Wi-Fi network card in the laptop, we must take into account Note that smartphones change more frequently, every two or three years, however, laptops we usually change every 5 or 7 years approximately.

Should I upgrade to a newer OS or a faster processor?

However, if you have a fast enough processor that is capable of both, choose the newer OS. This way, since the CPU is able to handle the OS well enough to run well, it is able to take advantage of the new software and increase the performance of the rest of the computer.


1 Answers

Naive version uses call to glibc sqrt function.

Optimized version uses SSE sqrtsd instruction. But after instruction has completed, it checks that result value is not a NaN. If result value is NaN then it calls glibc sqrt function to setup proper error flags (see manual page for math_error(7)). See Why does compiler generate additional sqrts in the compiled assembly code for detailed explanation.

Why gcc thinks that this is faster? Nobody knows. If you are sure that your numbers don't generate NaNs, use -fno-math-errno compile option.

like image 128
gudok Avatar answered Nov 15 '22 20:11

gudok