[quote=Fidodo;2348171]No geom is going to be slower. I like to use it because it’s a bit cleaner to pass a Point around as a vector than to pass around 2 coordinates all the time. In my example I’m creating a vector from a dot to the mouse and getting the magnitude. To me that seems more readable but that’s just my style of programming. Geom is definitely going to be slower but not by more than a couple extra instructions, and with processors doing millions of instructions per second that’s not going to effect anything.
I mean a 1.6 GHz processor does 1.6 billion calculations a second. Getting the distance from 2 points is absolutely nothing to the computer. The thing that is causing slowdown if there is any would be the rendering and other things the flash player does that you can’t really control as easily. Putting together a complex algorithm to save a couple thousand calculations is not worth it and kinda a waste of time. If you were doing something complex like hit detection then it would be worth optimizing it, but implementing the Pythagorean theorem is so simple that I’d just brute force it.[/quote]
This like that do matter, a lot. If you want to use a class instead of 2 variables at least write your own. Let me do a benchmark test right now to see the difference. And talking about stuff to increase speed, you should talk to jerryscript ( http://www.kirupa.com/forum/showthread.php?t=294827 ), things he does to increase speed is amazing xD
At least you are right about one thing, this isn’t the way to increase speed when you’re working with for example movielclips. However, not the fastest result equals bad coding.
EDIT: So I did the benchmark test. I made 3 methods, each method calculates the distance between 2 random chosen points for a selected number of times, for example 10000 times. Every method is as identical as possible, since length of variables matter (crazy AS2.0: http://www.kirupa.com/forum/showthread.php?t=292307 ) :S . Also note the absence of Math.sqrt!
** Here are the results:**
Using the Geom.Point class 10000 calculations took: 326 seconds
Using a custom made class 10000 calculations took: 251 seconds
Using a method 10000 calculations took: 221 seconds
As you can see there is quite a lot of difference. Using The Geom.Point class it almost takes 1.5 times as long. The custom class is a lot faster so if one doesn’t want to change the way one’s coding, advisable would be to recreate the Point class, it’s a one time job and saves time. Obviously this is still mostly irrelevant when stuff such as movieclips are being used.
Just a small note, flash is quite slow referencing to objects, so if one were to have one Math class containing methods to calculate things such as distance (I know I have one) it might take a little longer, it would still be a lot faster than the custom made class though.
For those interested the code (AS2.0, flashDevelop):
Main
import flash.geom.Point;
class Main
{
private static var numberOfTimes:Number = 10000;
static function main()
{
doUsingGeom();
doUsingClass();
doUsingMethod();
}
private static function doUsingGeom():Void
{
var startTime:Number = getTimer();
for (var i = 0; i < numberOfTimes; i ++)
{
var pnt:Point = new Point (rnm() - rnm(), rnm() - rnm());
var distance:Number = pnt.length;
}
var duration:Number = getTimer() - startTime;
trace ("Using the Geom.Point class " + numberOfTimes + " calculations took: " + duration + " seconds");
}
private static function doUsingClass():Void
{
var startTime:Number = getTimer();
for (var i = 0; i < numberOfTimes; i ++)
{
var loc:Location = new Location (rnm() - rnm(), rnm() - rnm());
var distance:Number = loc.Length();
}
var duration:Number = getTimer() - startTime;
trace ("Using a custom made class " + numberOfTimes + " calculations took: " + duration + " seconds");
}
private static function doUsingMethod():Void
{
var startTime:Number = getTimer();
for (var i = 0; i < numberOfTimes; i ++)
{
var distance:Number = getDistance(rnm(), rnm(), rnm(), rnm());
}
var duration:Number = getTimer() - startTime;
trace ("Using a method " + numberOfTimes + " calculations took: " + duration + " seconds");
}
private static function getDistance (x1:Number, y1:Number, x2:Number, y2:Number)
{
var xd:Number = x1 - x2;
var yd:Number = y1 - y2;
return ((xd*xd)+(yd*yd))^0.5;
}
private static function rnm ():Number
{
return Math.floor(Math.random()*(500));
}
}
And the custom made class (Location):
class Location
{
public var x:Number;
public var y:Number;
function Location(x:Number, y:Number)
{
this.x = x;
this.y = y;
}
public function Length ():Number
{
return ((x*x)+(y*y))^0.5;
}
}
Also some more usable threads on the subject of speed:
http://www.kirupa.com/forum/showthread.php?t=285243 (and the 2 links I apparently posted)
http://www.kirupa.com/forum/showthread.php?t=281686
http://www.kirupa.com/forum/showthread.php?t=276114 (and the 2 links already listed in this very post)
http://www.kirupa.com/forum/showthread.php?t=292307
http://www.kirupa.com/forum/showthread.php?t=294827