|
This thread is locked; no one can reply to it. |
1
2
|
Should I Use "sqrt(n)" or "pow(n,0.5f)"? |
Kris Asick
Member #1,424
July 2001
|
Occasionally, I notice things I can't believe I hadn't before. One of them just now is that taking a number to the power of 0.5 results in the same answer as taking the square root of that number. So my question is pretty simple: Which is the fastest for the CPU? sqrt(n); OR pow(n,0.5f); --- Kris Asick (Gemini) |
Evert
Member #794
November 2000
|
Try both and use a profiler to find out. My bet: sqrt(x) is faster than pow(x, 0.5) because there is a dedicated function for it (even if there wasn't one, sqrt(x) would be aliased to pow(x, 0.5) and therefore run at the same speed). |
gnolam
Member #2,030
March 2002
|
Use sqrt() for code clarity if nothing else. If you mean a square root, write it like a regular square root - just like you'd usually write instead of . -- |
bamccaig
Member #7,536
July 2006
|
I miss math class... -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Arthur Kalliokoski
Second in Command
February 2005
|
x86 has its own intrinsic sqrt function, which (at least at one time) was supposed to be faster than a division. They all watch too much MSNBC... they get ideas. |
Tobias Dammers
Member #2,604
August 2002
|
gnolam said: Use sqrt() for code clarity if nothing else.
Seconded. --- |
Evert
Member #794
November 2000
|
gnolam said: just like you'd usually write \sqrt{x} instead of x^{\frac{1}{2}} Funny, I tend to write the fractional exponent most of the time. It looks better if you have a very big expression under the root. I also deal with different fractional exponents and looks cleaner than in most cases. Let alone instead of . EDIT: but I always set it as , never . |
Jonatan Hedborg
Member #4,886
July 2004
|
sqrt is probably a LOT faster than pow(n, 0.5f). Specific algorithms versus generic ones... I would not be surprised if sqrt beats pow by an order of magnitude or more. Do some testing on it. Also, it's clearer.
|
gnolam
Member #2,030
March 2002
|
Evert said: I also deal with different fractional exponents and looks cleaner than in most cases. Let alone instead of .
That's why I added the "usually". -- |
Karadoc ~~
Member #2,749
September 2002
|
I almost exclusively write square root as an exponent when I work with them on paper - mostly because it looks neater. For example, I find it difficult to get a neat looking square root sign on the bottom of a faction, or a neat square root over a long expression. Never the less, I'd still use sqrt(x) rather than pow(x, 0.5) when I'm programming. ----------- |
StevenVI
Member #562
July 2000
|
I found this intriguing, so I went ahead and wrote my own test for it. The code: The result from gprof: % cumulative self self total time seconds seconds calls ms/call ms/call name 49.9 60.16 60.16 1231231231 0.00 0.00 p [2] 44.2 113.45 53.29 1231231231 0.00 0.00 s [3] 5.3 119.81 6.35 main [1] 1.3 121.33 1.52 _gmon_start_ [4] The self-seconds column is the important one here. I would conclude that there is hardly any difference. (I too use fractional exponents, by the way.) __________________________________________________ |
Evert
Member #794
November 2000
|
That test is flawed: to do a meaningful test, you need to compile with optimisations switched on, but since the results are never used, the compiler can (and will) eliminate the calculations altogether. |
Arthur Kalliokoski
Second in Command
February 2005
|
It'd also help if the calculations actually did useful work, so that any concurrency could be taken advantage of. They all watch too much MSNBC... they get ideas. |
torhu
Member #2,727
September 2002
|
Try something like this, compile with -O2 or -O3. You need one version for each function to test. Time it with the time command or something.
|
Oscar Giner
Member #2,207
April 2002
|
rand is not a fast function though. It may even be slower than sqrt so you're testing more rand than sqrt. -- |
torhu
Member #2,727
September 2002
|
Could be, but the point is to see what happens if you replace sqrt(rand()) with pow(rand(), 0.5); |
Oscar Giner
Member #2,207
April 2002
|
But that only allows you to know if one is faster than the other, but not by how much as Harry posted. -- |
Evert
Member #794
November 2000
|
Just calculate sqrt(i) or the result of some simple function of i. |
gillius
Member #119
April 2000
|
You also want to make sure the edge-case optimization is the same -- ideally you want the same input sets. It might make more sense to store the random numbers generated (to get around the 1 million array, you could make like a 100k array and loop it 10 times). That way you pass the same values to both. I would agree with Evert's first reply, though. If pow cannot be faster than sqrt, because if there was a way of doing pow faster than sqrt, the library implementer would call pow from sqrt. Of course, I assume a world with all of the same hardware. It theoretically could be possible that an implementation of sqrt that is faster than pow on some hardware could be slower on another hardware; however, I would imagine that to be an unlikely scenario. Gillius |
Kris Asick
Member #1,424
July 2001
|
Geeze... once again I've proven that all I have to do is say something and people will debate for hours! Since I only have to make the call 200 times a second, and because I wanted fine control over the shape of the curve, I decided to use pow() with a constant so that I can just change the constant if I feel the exponential curve is too shallow or too steep. But, don't let that stop any of you from continuing to explore which is better for square roots. (Note to Mods: "proven" is missing from the spell check dictionary.) --- Kris Asick (Gemini) |
Arthur Kalliokoski
Second in Command
February 2005
|
Your original post lead us to believe the needed square root was time critical. They all watch too much MSNBC... they get ideas. |
Evert
Member #794
November 2000
|
Kris Asick said: Since I only have to make the call 200 times a second, and because I wanted fine control over the shape of the curve, I decided to use pow() with a constant so that I can just change the constant if I feel the exponential curve is too shallow or too steep. But, don't let that stop any of you from continuing to explore which is better for square roots. That is a bit rich, coming from the guy who asked Kris Asick said: So my question is pretty simple: Which is the fastest for the CPU? sqrt(n); OR pow(n,0.5f);
|
Speedo
Member #9,783
May 2008
|
It may be implementation dependent, but at least on MSVC9 sqrt() appears to be significantly faster than pow(). I would generally advocate using sqrt() regardless just for readability. Test: 1#include <cmath>
2#include <ctime>
3#include <iostream>
4
5int main( )
6{
7 const int loops = 10000000;
8 float f;
9
10 std::time_t start = std::clock( );
11
12 for (int i = 0; i < loops; ++i)
13 f = std::sqrt(static_cast<float>(i));
14
15 std::cout << std::clock( ) - start << std::endl;
16 std::cout << f << std::endl;
17
18 start = std::clock( );
19
20 for (int i = 0; i < loops; ++i)
21 f = std::pow(static_cast<float>(i), 0.5f);
22
23 std::cout << std::clock( ) - start << std::endl;
24 std::cout << f << std::endl;
25
26 return 0;
27}
Average time across 10 tests: Edit: Tests are with optimization. |
Kris Asick
Member #1,424
July 2001
|
Evert said: That is a bit rich, coming from the guy who asked Maybe, but I was mostly just curious because it's not something I ever put any thought into and was wondering if someone else already knew. I think it's easy to forget with all the other problems which come up on this forum that not everyone has critical issues. If CPU time was a critical factor I would've tested this myself and never asked. I'm still kinda surprised how many people responded to a question I thought was really simple and unimportant. --- Kris Asick (Gemini) |
BlackShark
Member #9,796
May 2008
|
Give this Square root function a go, float SquareRoot(float number) { long i; float x, y; const float f = 1.5F; x = number * 0.5F; y = number; i = * ( long * ) &y; i = 0x5f3759df - ( i >> 1 ); //Black magic mystery number y = * ( float * ) &i; y = y * ( f - ( x * y * y ) ); y = y * ( f - ( x * y * y ) ); return number * y; }
-BlackShark |
|
1
2
|