Loader って使い回せるのかテスト
DisplayObject系は使い回すための手動初期化に時間かかりすぎる
/**
* Copyright yasurageruheya ( http://wonderfl.net/user/yasurageruheya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/W90e
*/
package {
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.display.Graphics;
import flash.utils.getTimer;
import flash.display.Loader;
public class FlashTest extends Sprite {
private var txt:TextField;
private var pooler:LoaderPooler;
private const COUNT:int = 10000;
public function FlashTest() {
// write as3 code here..
txt = new TextField();
txt.autoSize = "left";
addChild(txt);
var spr:Sprite = createBtn("one more test", 0, 350);
spr.addEventListener(MouseEvent.CLICK, test);
addChild(spr);
pooler = new LoaderPooler();
test();
}
private function test(e:MouseEvent = null):void
{
var str:String = "テスト回数 : " + COUNT;
var time:int;
var i:int;
var loaders:Vector.<Loader> = new Vector.<Loader>(COUNT);
time = getTimer();
i = COUNT;
while (i--)
{
loaders[i] = new Loader();
//loaders[i].cacheAsBitmap = true;
}
str += "\nLoader を new するのにかかった時間 : " + (getTimer() - time) + " msec";
time = getTimer();
i = COUNT;
while (i--)
{
pooler.dispose(loaders[i]);
}
str += "\nLoader を 手動で初期化するのにかかった時間 : " + (getTimer() - time) + " msec";
time = getTimer();
i = COUNT;
var ldr:Loader;
while (i--)
{
ldr = pooler.getLoader();
}
str += "\nLoader を プールから取り出すのにかかった時間 : " + (getTimer() - time) + " msec";
time = getTimer();
i = COUNT;
while (i--)
{
ldr = new Loader();
}
str += "\n単純に new するのにかかった時間 : " + (getTimer() - time) + " msec";
txt.text = str;
}
private function createBtn(str:String, x:int, y:int):Sprite
{
var txt:TextField = new TextField();
var spr:Sprite = new Sprite();
txt.autoSize = "left";
txt.textColor = 0xFFFFFF;
txt.text = str;
txt.selectable = false;
spr.addChild(txt);
var g:Graphics = spr.graphics;
g.beginFill(0, 1);
g.drawRect(0, 0, spr.width, spr.height);
g.endFill();
spr.x = x;
spr.y = y;
spr.buttonMode = true;
addChild(spr);
return spr;
}
}
}
import flash.display.Bitmap;
import flash.display.Loader;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
class LoaderPooler
{
private var pool:Vector.<Loader>;
private static const _ldr:Loader = new Loader();
private static const filters:Array = _ldr.filters;
private static const matrix:Matrix = _ldr.transform.matrix;
private static const colorTransform:ColorTransform = _ldr.transform.colorTransform;
public function dispose(ldr:Loader):void
{
ldr.unloadAndStop();
ldr.accessibilityImplementation = null;
ldr.accessibilityProperties = null;
ldr.blendMode = "normal";
ldr.contextMenu = null;
ldr.doubleClickEnabled = false;
ldr.focusRect = null;
ldr.mask = null;
ldr.mouseChildren = true;
ldr.mouseEnabled = true;
ldr.opaqueBackground = null;
ldr.scale9Grid = null;
ldr.scrollRect = null;
ldr.tabChildren = true;
ldr.tabEnabled = false;
ldr.tabIndex = -1;
ldr.transform.matrix = matrix;
ldr.transform.colorTransform = colorTransform;
ldr.visible = false;
// cacheAsBitmap を false にするために、まず filters を初期化
ldr.filters = filters;
// cacheAsBitmap で作られてた Bitmap が使ってたメモリも開放するためにちょっと頑張る
if (ldr.cacheAsBitmap)
{
var bmp:Bitmap = ldr.getChildAt(0) as Bitmap;
bmp.bitmapData.dispose();
bmp.bitmapData = null;
ldr.cacheAsBitmap = false;
}
pool.push(ldr);
}
public function getLoader():Loader
{
if (pool.length)
{
var ldr:Loader = pool.pop();
ldr.visible = true;
return ldr;
}
else
{
return new Loader();
}
}
public function LoaderPooler()
{
pool = new Vector.<Loader>();
}
}