Ahmed Nuaman
Ahmed Nuaman

Reputation: 13221

Benchmarking code in AS3

Does anyone have any tips or links to libraries to help with benchmarking applications in AS3? I'm (hopefully) looking for something along the lines of Benchmark.js but for Flash and AIR. Any suggestions welcome.

Upvotes: 1

Views: 1637

Answers (5)

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

Building on lostPixels' answer, I created a function that is similar to Python's timeit() function. The function repeats the callback function for the number of iterations specified and returns the fastest execution time. The default is 1,000,000 iterations.

The following Test program ran in about 391ms on my machine. Without the trace() statements, the test takes less than 1ms to execute.

TimeIt.as

package {
  public class TimeIt {
    import flash.utils.getTimer;

    public static function timeIt(callback:Function, maxIterations:uint=1000000):int {
      var start_time:int, duration:int, fastest_time:int = int.MAX_VALUE;
      for (var i:int = 0; i < maxIterations; i++) {
        start_time = getTimer();
        callback();
        duration = getTimer() - start_time;
        if (duration < fastest_time) fastest_time = duration
      }
      return fastest_time;
    }
  }
}

Test.as

package {
  public class Test {
    public function Test() {
      trace('Fastest Time:', TimeIt.timeIt(test, 10),'ms');
    }

    public function test():void {
      var k:int, m:int = 100;
      for (var i:int = 0; i < m; i++) {
        for (var j:int = 0; j < m; j++) {
          k = m * i + j;
          trace(k);
        }
      }
    }
  }
}

Main.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
    xmlns:s="library://ns.adobe.com/flex/spark" 
    initialize="init(event)">
    <fx:Script>
        <![CDATA[
            protected function init(event:Event):void {
                new Test();
            }
        ]]>
    </fx:Script>
</s:Application>

Upvotes: 0

Marty
Marty

Reputation: 39456

You can set up a benchmarking method quite easily:

function test(process:Function, repeat:int = 10):void
{
    var time:Number = getTimer();
    while(--repeat >= 0) process();
    trace(getTimer() - time);
}

Used like so:

// See how long it takes to create 50,000 Sprites and
// add them to the DisplayList.
test(function()
{
    var sprite:Sprite = new Sprite();
    addChild(sprite);

}, 50000);

Upvotes: 0

Josh
Josh

Reputation: 8149

This isn't really meant for benchmarking, but Adobe Scout is a fantastic profiler/performance tester. I've been using it for everything from SWFs for the web to Adobe AIR apps to mobile AIR apps.

Upvotes: 3

lostPixels
lostPixels

Reputation: 1343

A quick way to measure code execution time that I often times use is:

var start_time:int = getTimer();

someComplexCode();

trace("execution time: ", getTimer()-start_time);

This will give you a number in milliseconds.

Upvotes: 4

Ambar
Ambar

Reputation: 112

You can try using this: Performance Test. Also, I found some good information here.

Upvotes: 1

Related Questions