Jump to content
• Advertisement

#### Archived

This topic is now archived and is closed to further replies.

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

This topic is 5897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

which is faster? result=a*a; or result=sqrt(a); (yes, square root, not square)

#### Share this post

##### Share on other sites
Advertisement
a*a should be faster, because sqrt(a) includes a bit of guessing i think.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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.

#### Share this post

##### Share on other sites
You wouldn''t call it guessing. It''s approximation.

#### Share this post

##### Share on other sites
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().

#### Share this post

##### Share on other sites
I agree with siaspete. The multiply would be faster because the is no function calling.

#### Share this post

##### Share on other sites
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

#### Share this post

##### Share on other sites
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]

#### Share this post

##### Share on other sites
quote:
Original post by Anonymous Poster
[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.

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

#### Share this post

##### Share on other sites

• Advertisement

### Announcements

• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
Rutin
17
5. 5
• Advertisement

• 11
• 37
• 12
• 12
• 11
• ### Forum Statistics

• Total Topics
631413
• Total Posts
2999945
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!