Post

 Resources 

Console

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

 All Forums
 VBGamer
 VBGamer
 Square root speeds

Note: You must be registered in order to post a reply.

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List Spell Checker
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

   Insert an File
Check here to include your profile signature.
Check here to subscribe to this topic.
    

T O P I C    R E V I E W
Threshold Posted - Dec 13 2004 : 2:01:51 PM
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?
4   L A T E S T    R E P L I E S    (Newest First)
Walrus Posted - Dec 16 2004 : 04:45:41 AM
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.
cbx Posted - Dec 15 2004 : 12:42:35 PM
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.
cbx Posted - Dec 14 2004 : 05:00:56 AM
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.
Threshold Posted - Dec 13 2004 : 2:26:56 PM
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.

VBGamer © Go To Top Of Page
This page was generated in 0.11 seconds. Snitz Forums 2000

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