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).
The algorithm was approximately four times faster than computing the square root with another method and calculating the reciprocal via floating-point division.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With