which is faster? a*a or sqrt(a)

Started by
21 comments, last by iwaskia 21 years, 11 months ago
which is faster? result=a*a; or result=sqrt(a); (yes, square root, not square)
Advertisement
a*a should be faster, because sqrt(a) includes a bit of guessing i think.
a*a by far.

Even if the sqrt function didn''t do anything the multiply would be faster.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
quote:Even if the sqrt function didn''t do anything the multiply would be faster.


not quite. if you had sqrt mapped to do nothing (NOOP, 0, NULL,etc.. ). nothing would be done. nothing is faster than a multiply.

alienid..there is no guessing in the sqrt function. if your doing something that needs absolute precision, you wouldnt be using sqrt anyways.

You wouldn''t call it guessing. It''s approximation.
Kill mages first!
AP, even if sqrt() did nothing at all except return a constant float (since at the very least it MUST do this in order to be valid in this context), at best you can get similar speeds. at worst, its much slower (ie the compiler does not inline the function). no inline means at minium you will have at least a CALL, NOP, RET, which under the hood will push the current address onto the stack and pop the address for the return jmp when done. also you will have to push the arg, and pop the result. much more then just doing a multiplications, i would say. if you dont believe me, code a simple test app that uses QueryPerformenceCounter(). just make sure you are fair and have both methods start at 0.0 and increment a.

AP, the problem is you dont understand function calls, nor how cpus work. i highly suggest learning ask and about compilers before spewing false information.

A*A is faster then sqrt().
I agree with siaspete. The multiply would be faster because the is no function calling.
I''m not going to go into detail here...but the basic algorithim for finding square roots is like a more complex version of long division.

However, you could implement a approximation using ''cut and try''
IE, take a number, multiply it by itself, see if its near the number you need, if its not, find another number, if it is, jump , if that is further away, jump in the other direction.

If you are do a num*num, that is at most 3 lines of asm, I think.
Its a simple mul instruction, and a assignment.

So it obvoius that the sqr is faster.
Prsonally, I have a #define sqr directive- I`ve had to use num*num so much,
It goes like so....

#define sqr(num) num*num

:D

How to make a fast sqrt, I dont know. Sorry !

Bugle4d
~V'lionBugle4d
Finding the square root is very slow but there is a x87 math assembly instruction, fsqrt, to do it, which is at least faster than any software implementations. But it's still slow.

A mere square, on the other hand, is just a multiplication and is much faster.

~CGameProgrammer( );

EDIT: The sqrt() function almost definitely calls the assembly command.

[edited by - CGameProgrammer on April 30, 2002 1:48:00 AM]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
quote:Original post by Anonymous Poster
Even if the sqrt function didn''t do anything the multiply would be faster.


not quite. if you had sqrt mapped to do nothing (NOOP, 0, NULL,etc.. ). nothing would be done. nothing is faster than a multiply.

alienid..there is no guessing in the sqrt function. if your doing something that needs absolute precision, you wouldnt be using sqrt anyways.



Why would you do that.
Heas asking if, ie, 3*3 and sqrt(4), not nothing*nothing and sqrt(nothing)

[ my engine ][ my game ][ my email ]
SPAM
Rate me up.

This topic is closed to new replies.

Advertisement