Post

 Resources 

Console

Home | Profile | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 VBGamer
 VBGamer
 Square root speeds
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Threshold
Squire

USA
44 Posts

Posted - Dec 13 2004 :  2:01:51 PM  Show Profile  Reply with Quote
Which is faster, Sqrt(X) or X^0.5? What about other methods or ASM equivalents?

I need the fastest method possible, specifically for a Distance(X, Y, X2, Y2) function.

Any thoughts? Ideas? Experience?

Life is short. They say "don't waste it, have fun." They're right, don't waste it...but DO redefine "fun."

Threshold
Squire

USA
44 Posts

Posted - Dec 13 2004 :  2:26:56 PM  Show Profile  Reply with Quote
I did a little test, and it's pretty obvious that Sqrt(X) (just Sqr in VB 6.0) is 9 times faster than X^0.5.

Here are the numbers I got on my computer (2.3 GHz):

Sqrt(X) - 0.00004186 ms
X^0.5 - 0.00038296 ms

(This is with the compiled EXE. Compiled for fast code with all checkboxes in the Advanced Options window unchecked.)

I thought it would be the other way arround, but I guess it makes sense that the Sqrt function is optimized for square roots unlike the ^ operator.

Here is the code I used for the test (somewhat of a hack, sorry):

  
Option Explicit
  
Private Const ITERATIONS As Long = 50000000  
  
Private Const VAL As Double = 44.4444444444444  
  
Private Declare Function GetTickCount Lib "kernel32" () As Long
  
Public Sub Main()  
Dim i As Long
Dim dummy As Double
Dim Timer As Double
Dim lStart As Long
Dim lEnd   As Long
  
i = 1  
  
lStart = GetTickCount  
For i = 1 To ITERATIONS  
    dummy = Sqr(VAL)  
Next i  
lEnd = GetTickCount  
  
Timer = (lEnd - lStart) / ITERATIONS  
Call MsgBox("Sqrt(i) takes approximately (on average) " & Timer & " milliseconds to compute (with " & CStr(ITERATIONS) & " iterations).", vbInformation, "Sqrt(i)")  
  
i = 1  
  
lStart = GetTickCount  
For i = 1 To ITERATIONS  
    dummy = VAL ^ 0.5  
Next i  
lEnd = GetTickCount  
  
Timer = (lEnd - lStart) / ITERATIONS  
Call MsgBox("i^0.5 takes approximately (on average) " & Timer & " milliseconds to compute (with " & CStr(ITERATIONS) & " iterations).", vbInformation, "i^0.5")  
  
End
End Sub
  


I would still appreciate any feedback or other ideas.


Life is short. They say "don't waste it, have fun." They're right, don't waste it...but DO redefine "fun."

Edited by - Threshold on Dec 13 2004 2:28:04 PM
Go to Top of Page

cbx
Swordmaster

Canada
296 Posts

Posted - Dec 14 2004 :  05:00:56 AM  Show Profile  Visit cbx's Homepage  Send cbx an ICQ Message  Click to see cbx's MSN Messenger address  Send cbx a Yahoo! Message  Reply with Quote
quote:
Originally posted by Threshold
<br>Which is faster, Sqrt(X) or X^0.5? What about other methods or ASM equivalents?

I need the fastest method possible, specifically for a Distance(X, Y, X2, Y2) function.

Any thoughts? Ideas? Experience?



If you are needing to calculate the distance to move a character but that character only contains a speed, position, and direction properties then you could pre compute the distance to move and store the values in an array.

For example say you are making a 2D top down game, And you have a character that only contains speed, position, and direction properties. the direction property would be from 0 to 359 and would represent the angle in degrees, that the character if facing.

You could create an 360 indexed array and store pre-computed distance values for each index in the array. But the distance you will compute will only be a distance of 1. So you will end up with an array of 360 items each item in the array would represent the offset or distance to move the character from it's current position. So for example if the character is standing at position 10x12 and is facing 90 degrees (to the right) you would simply add the values in the precomputed array to the characters position and as such the character will then be standing at position 11x12.

Character Position = Character Position + direction array(90)
or
character Position = 10x12 + 1x0

Now if you want to move your character farther then a distance of 1 you simply multiply the values from the direction array by the characters speed, to get the new position of the character.

Character Speed = 12.3
Character Position = Character Position + (direction array(90) * Character Speed)
or
character Position = 10x12 + ((1 * Character Speed)x(0 * Character Speed))

This process should help speed up your code. But I'm guessing that if you really want to speed up your code you could track down the actual math equation to calculate the square root of a number and use that.

Code often executes faster if you perform the calculations by hand rather then calling a method to perform the math for you, like the Math.Sqrt (just Sqr in VB 6.0) methods for example.

Created by: X
http://www.createdbyx.com/

Edited by - cbx on Dec 14 2004 05:05:12 AM
Go to Top of Page

cbx
Swordmaster

Canada
296 Posts

Posted - Dec 15 2004 :  12:42:35 PM  Show Profile  Visit cbx's Homepage  Send cbx an ICQ Message  Click to see cbx's MSN Messenger address  Send cbx a Yahoo! Message  Reply with Quote
quote:
Originally posted by Threshold
<br>Which is faster, Sqrt(X) or X^0.5? What about other methods or ASM equivalents?

I need the fastest method possible, specifically for a Distance(X, Y, X2, Y2) function.

Any thoughts? Ideas? Experience?



I mentioned in my previous post about how you could calculate the square root of a number by hand, and that doing so may help speed up your code further. But I did not tell you how this is done. Here is a link to a great site http://mathforum.org/dr.math/faq/faq.sqrt.by.hand.html

I have solved/understood many math problems with the help of this site.

Created by: X
http://www.createdbyx.com/

Edited by - cbx on Dec 15 2004 12:43:06 PM
Go to Top of Page

Walrus
Squire

Slovenia
34 Posts

Posted - Dec 16 2004 :  04:45:41 AM  Show Profile  Reply with Quote
Also, the sqr function(VB 6.0) returns a double. It depends on what you're trying to do, but you probably don't need such a precise result. So you can use one of the methods to calculate just an approximation, which might do the trick just as well.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
VBGamer © Go To Top Of Page
This page was generated in 0.16 seconds. Snitz Forums 2000

Copyright © 2002 - 2004 Eric Coleman, Peter Kuchnio , et. al.