Square root speeds Threshold 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? Threshold 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): [code] 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 [/code] I would still appreciate any feedback or other ideas. cbx quote:Originally posted by Threshold
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. cbx quote:Originally posted by Threshold
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 [url]http://mathforum.org/dr.math/faq/faq.sqrt.by.hand.html[/url] I have solved/understood many math problems with the help of this site. Walrus 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.