Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / C++ / C# - Fast Square Root...

Author
Message
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 5th Feb 2011 00:31 Edited at: 5th Feb 2011 00:36
I came across this when I was in the process of optimizing parts of my code I've been working on:

http://ilab.usc.edu/wiki/index.php/Fast_Square_Root

Part of it gives an example:



Take 1/5 the time to process than sqrt().

It does give pretty good (but dirty) accuracy for calculating distance between objects. There are more accurate versions on the web page which still give faster results than "math.h" sqrt() function.

Warning! May contain Nuts!
PrimalBeans
14
Years of Service
User Offline
Joined: 14th Oct 2010
Location: The sewer.... hunting alligatiors.
Posted: 5th Feb 2011 00:42
Am i right in assuming that the real advantage to this is using an
inline function?? (If i remember right inline types get basically cut and pasted into code during runtime?)

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 5th Feb 2011 00:44 Edited at: 5th Feb 2011 00:46
Yes, I've been editing quite a bit lately with inline and seen a slight increase in fps over a range of my coding tests. Usually I only inline if a function is no less than five lines of code. But that's just the way I do it. Lack of stack pushing and popping decreases clock time on the cpu.

Warning! May contain Nuts!
PrimalBeans
14
Years of Service
User Offline
Joined: 14th Oct 2010
Location: The sewer.... hunting alligatiors.
Posted: 7th Feb 2011 23:43 Edited at: 7th Feb 2011 23:44
well that makes me feel like im not a complete noob then. lol. It is my understanding that inline functions are great for simple one or two line tasks that need to be repeated alot, but can cause clutter if not used sparingly. Thanks for the info.


or maybe i have it backwards....

puppyofkosh
18
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 8th Feb 2011 02:06
If you are comparing square roots (like distances), I think you can just compare the squares for better performance.
WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 8th Feb 2011 13:33
@PrimalBeans

Using inline functions doesn't necessarily clutter your code because it's only really adding the "inline" infront of your function. I've used it since the days of the Atari ST for optimization. It will increase the code size a little but I never really bother with that because to write something that will take up all of t=your memory would take forever anyway lol

@puppyofkosh

I've been experimenting with all kinds of speed ups with maths functions. So far with no luck but somethings have come out of it. The square root function I've been using for distances. Testing it out on my laptop it did increase performance. Although the accuracy isn't great it works perfect for what I'm after. I was thinking of using a lookup table to start with until I came across this. But then a loop up table would need to be so big anyway. Being an old 68000 coder I like cutting down on cpu clock cycles. Just a habit.

Warning! May contain Nuts!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 8th Feb 2011 22:28
I think you missed his point, which was not doing a square root is faster than doing one.

If you are comparing distances, for example finding the nearest object or sorting objects by distance then you don't need to find the square root of your number - you simply use the same equation without the square root. It's only if you need to know the actual distance, such as when showing the distance to a target, that you would carry out the square root.

WLGfx
17
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 9th Feb 2011 00:57
Thanks IanM. I've always assumed doing a square root was the be all and end all but your explanation of sorting objects has sunk in. In my most recent code I was checking whether an object was within a certain range. That way the enemy could react in some way. Also the enemies would also try to react whenever a bullet was in range and within the view, and there's lots of bullets flying around. I will certainly remember that one now though. Cheers

Warning! May contain Nuts!

Login to post a reply

Server time is: 2025-05-19 03:45:27
Your offset time is: 2025-05-19 03:45:27