转自:http://www.kingnare.com/auzn/archives/546
文章对Grahpics API新增的drawPath, drawTriangles方法与传统方法之间的性能做了一些测试.
运行下面的代码:
代码
var container:Shape = new Shape();
addChild(container);
var currentTime:Number = getTimer();
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
container.graphics.drawPath(commands, coords);
}
trace( getTimer() - currentTime );
addChild(container);
var currentTime:Number = getTimer();
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
container.graphics.drawPath(commands, coords);
}
trace( getTimer() - currentTime );
用时18~20ms
然后用传统的方法实现:
代码
var container:Shape = new Shape();
addChild(container);
var currentTime:Number = getTimer();
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
//container.graphics.drawPath(commands, coords);
container.graphics.moveTo( coords[0], coords[1] );
container.graphics.lineTo( coords[2], coords[3] );
container.graphics.lineTo( coords[4], coords[5] );
container.graphics.lineTo( coords[6], coords[7] );
container.graphics.lineTo( coords[8], coords[9] );
}
trace( getTimer() - currentTime );
addChild(container);
var currentTime:Number = getTimer();
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
//container.graphics.drawPath(commands, coords);
container.graphics.moveTo( coords[0], coords[1] );
container.graphics.lineTo( coords[2], coords[3] );
container.graphics.lineTo( coords[4], coords[5] );
container.graphics.lineTo( coords[6], coords[7] );
container.graphics.lineTo( coords[8], coords[9] );
}
trace( getTimer() - currentTime );
竟和原文一样,用时31ms
原文提到,虽然新方法快了不少,但有一点要注意,我们所测试的是把所有指令数据传送给renderer的时间,并不包括rendering time.
通过以上测试,得知方法drawPath快很多,但我们并不知道所使用的rendering time是多少.
Flash Player的rendering是异步的,所以要注意在帧重绘中才会引发rendering.
强行调用Flash Player的renderer就是使用BitmapData.draw(). 可以通过这个方法测试rendering时间.
代码
var bitmap:BitmapData = new BitmapData(400,400);
var container:Shape = new Shape();
addChild(container);
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
container.graphics.drawPath(commands, coords);
}
var currentTime:Number = getTimer();
bitmap.draw( container );
trace( getTimer() - currentTime );
var container:Shape = new Shape();
addChild(container);
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
container.graphics.drawPath(commands, coords);
}
var currentTime:Number = getTimer();
bitmap.draw( container );
trace( getTimer() - currentTime );
多次运行的结果为0或1,不过0的机率要大些.
使用传统方法测试,结果差不多.
最后,总的时间花销为:
代码
var bitmap:BitmapData = new BitmapData(400,400);
var container:Shape = new Shape();
addChild(container);
var currentTime:Number = getTimer();
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
container.graphics.drawPath(commands, coords);
}
bitmap.draw( container );
trace( getTimer() - currentTime );
var container:Shape = new Shape();
addChild(container);
var currentTime:Number = getTimer();
var commands:Vector. = Vector.([1,2,2,2,2]);
var coords:Vector. = Vector.([10,10,100,10,100,100,10,100,10,10]);
for (var i:int = 0; i< 15000; i++)
{
container.graphics.clear();
container.graphics.beginFill(0x990000, 1);
container.graphics.drawPath(commands, coords);
}
bitmap.draw( container );
trace( getTimer() - currentTime );
用时20~22ms
作者结论是从AS代码执行来讲,drawPath,drawTriangles能提供更高的性能.
如果你的程序将大量时间花销在向renderer发送命令数据上,可以使用此方法来提高程序性能;
但对于rendering的性能提升,新的API是不会有任何帮助的.