Threshold
Squire
USA
44 Posts |
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? |
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 |
|
cbx
Swordmaster
Canada
296 Posts |
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. |
Created by: X http://www.createdbyx.com/ |
Edited by - cbx on Dec 14 2004 05:05:12 AM |
|
|
cbx
Swordmaster
Canada
296 Posts |
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. |
Created by: X http://www.createdbyx.com/ |
Edited by - cbx on Dec 15 2004 12:43:06 PM |
|
|
Walrus
Squire
Slovenia
34 Posts |
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. |
|
|
|
|