449,239 Members | 1,177 Online
Need help? Post your question and get tips & solutions from a community of 449,239 IT Pros & Developers. It's quick & easy.

# Square a float: pow or f*f?

 P: n/a Any on consensus on which of these is faster? inline float Square1(float f) { return std::pow(f, 2.0f); } inline float Square2(float f) { return f*f; } Chris Jun 27 '08 #1
4 Replies

 P: n/a On Jun 21, 5:41 pm, "Alf P. Steinbach"

 P: n/a On Jun 22, 3:07 am, chrisstankev...@gmail.com wrote: On Jun 21, 5:41 pm, "Alf P. Steinbach"

 P: n/a James Kanze wrote: The answer is that it will depend on the machine and the compiler (although typically, I would expect a*a to be faster). The answer is also that even in a tight loop which does nothing else, the difference is likely to be insignificant. At least on Intel processors a pow() will be inherently slower than a multiplication. However, many compilers are able to optimize a "std::pow(d, 2.0)" call into "d*d". I tested this on my computer, using the program below, using gcc 4.1.2 with the compiler options "-O3 -march=pentium4 -s" and I got these results: Time: 1.69 s, result = 2.66667e+18 Time: 1.69 s, result = 2.66667e+18 Time: 47.72 s, result = 2.69852e+18 The first and second tests show no difference, so clearly gcc is optimizing the pow() call away. The third version forces gcc to perform a true pow() call, and it's a lot slower. #include #include #include inline double square1(double d) { return d*d; } inline double square2(double d) { return std::pow(d, 2.0); } inline double square3(double d) { return std::pow(d, 2.001); } template void test(F f) { clock_t t1 = std::clock(); double res = 0, d = .001; for(int i = 0; i < 200000000; ++i) { res += f(d); d += .001; } clock_t t2 = std::clock(); std::cout << "Time: " << int((t2-t1)*100.0/CLOCKS_PER_SEC)/100.0 << " s, result = " << res << std::endl; } int main() { test(square1); test(square2); test(square3); } Jun 27 '08 #4

 P: n/a On Jun 22, 8:45 am, Juha Nieminen

### This discussion thread is closed

Replies have been disabled for this discussion.