Chunky Chunk
Chunky Chunk

Reputation: 17237

ActionScript - Profile Speed Of Script From Code?

i'm trying to cave-man-profile the speed of my code from code using the date class and trace statements. it's not working out so well.

package
{   
import flash.display.Sprite;

public class Test extends Sprite
    {
    public function Test()
        {
        var now:Date = new Date();
        var profileSpeedMark:Number = now.getMilliseconds();

        var myArray:Array = new Array();
        for (var i:Number = 0; i < 1000000; i++)
            myArray.push(Math.random());

        var profileSpeedResult:Number = now.getMilliseconds() - profileSpeedMark;
        trace(profileSpeedResult);
        }
    }
}

the idea here is to be able to compare different ways of coding functions and determine which one is faster. unless i'm on a super slow computer i'm not sure if this is possible, especially for really short functions.

Upvotes: 0

Views: 186

Answers (2)

user235273
user235273

Reputation:

Its convenient to use getTimer() function in the flash.utils package.

var t0:Number = getTimer();
computeSomeThing();
var t1:Number = getTimer();
trace("Time Elapsed: " + String(t1 - t0));

Upvotes: 3

Andrew Aylett
Andrew Aylett

Reputation: 40760

The problem with the way you've done things in your example code is that the Date class has a fixed value -- you're comparing the time before the tests with the time before the tests again. Try instantiating another Date object after your tests and taking the time from that:

var before:Date = new Date();
test();
var after:Date = new Date();
var timeTaken:Number = after.time - before.time;

Upvotes: 2

Related Questions