AS3中的基准测试代码



谁有任何提示或链接到库,以帮助在AS3基准测试应用程序?我(希望)寻找的东西沿着基准的线,但Flash和AIR。

我经常使用的一种快速测量代码执行时间的方法是:

var start_time:int = getTimer();
someComplexCode();
trace("execution time: ", getTimer()-start_time);

这将给你一个以毫秒为单位的数字。

这并不是为了基准测试,但是Adobe Scout是一个非常棒的分析器/性能测试器。从web swf到Adobe AIR应用程序再到移动AIR应用程序,我一直在使用它。

您可以尝试使用这个:性能测试。另外,我在这里找到了一些很好的信息。

您可以很容易地设置基准测试方法:

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

像这样使用:

// 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);

基于lostPixels的回答,我创建了一个类似于Python的timeit()函数的函数。该函数按照指定的迭代次数重复回调函数,并返回最快的执行时间。默认为1,000,000次迭代。

下面的测试程序在我的机器上运行大约391ms。如果没有trace()语句,测试执行时间小于1ms

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;
    }
  }
}

试验原理

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>

最新更新