有没有人有任何提示或库链接来帮助在 AS3 中对应用程序进行基准测试?我(希望)正在寻找与Benchmark.js类似的东西,但要寻找 Flash 和 AIR。欢迎任何建议。
5 回答
我经常使用的一种快速测量代码执行时间的方法是:
var start_time:int = getTimer();
someComplexCode();
trace("execution time: ", getTimer()-start_time);
这将为您提供以毫秒为单位的数字。
这并不是真正的基准测试,但Adobe Scout是一个出色的分析器/性能测试器。从 Web 的 SWF 到 Adobe AIR 应用程序再到移动 AIR 应用程序,我一直在使用它。
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>
您可以很容易地设置基准测试方法:
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);