なんでもかんでもプールした方が早いのか
なんでもかんでも早いのかもしれない
♥0 |
Line 126 |
Modified 2011-05-31 05:23:15 |
MIT License
archived:2017-03-20 08:07:35
ActionScript3 source code
/**
* Copyright yasurageruheya ( http://wonderfl.net/user/yasurageruheya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/k6nQ
*/
package {
import flash.events.MouseEvent;
import flash.display.Graphics;
import flash.text.TextField;
import flash.display.Sprite;
import flash.utils.getTimer;
[SWF(width="465", height="465")]
public class Main extends Sprite
{
private var intRect:IntRectangle;
private var intRectMngr:IntRectManager;
private var _txt1:TextField;
private var _txt2:TextField;
private var _txt3:TextField;
private const TEST_COUNT:int = 100000;
public function Main() {
// write as3 code here..
_txt1 = new TextField();
_txt2 = new TextField();
_txt3 = new TextField();
_txt2.y = 20;
_txt3.y = 40;
_txt1.autoSize = _txt2.autoSize = _txt3.autoSize = "left";
addChild(_txt1);
addChild(_txt2);
addChild(_txt3);
intRectMngr = new IntRectManager();
var _btn:Sprite = new Sprite();
var _btn_txt:TextField = new TextField();
_btn_txt.autoSize = "left";
_btn_txt.text = "One more Test";
_btn_txt.textColor = 0xFFFFFF;
_btn_txt.selectable = false;
var g:Graphics = _btn.graphics;
g.beginFill(0,1);
g.drawRect(0,0, _btn_txt.width, _btn_txt.height);
g.endFill();
_btn.addChild(_btn_txt);
_btn.y = 70;
addChild(_btn);
_btn.useHandCursor = true;
_btn.addEventListener(MouseEvent.CLICK, test);
test();
}
private function test(e:MouseEvent = null):void
{
var i:int = TEST_COUNT;
var time:int = getTimer();
while(i--)
{
intRect = new IntRectangle();
}
_txt1.text = String(getTimer() - time) + ": new にかかった時間";
i = TEST_COUNT;
time = getTimer();
while(i--)
{
intRect = intRectMngr.getIntRect();
intRect.dispose();
}
_txt2.text = String(getTimer() - time) + ": プールから取り出すのにかかった時間";
i = TEST_COUNT;
time = getTimer();
while(i--)
{
intRect = new IntRectangle();
}
_txt3.text = String(getTimer() - time) + ": 一応もっかい new するのにかかった時間";
}
}
}
class IntRectManager
{
public var pool:Vector.<IntRectangle>;
public function getIntRect():IntRectangle
{
var intRect:IntRectangle;
if(pool.length)
{
intRect = pool.pop();
}
else
{
intRect = new IntRectangle(0,0,0,0,this);
}
return intRect;
}
public function buffer(count:uint):void
{
while(count--)
{
pool.push(new IntRectangle());
}
}
public function IntRectManager()
{
pool = new Vector.<IntRectangle>();
}
}
class IntRectangle
{
public var x:int;
public var y:int;
public var width:int;
public var height:int;
private var parent:IntRectManager;
public function clone():IntRectangle
{
return new IntRectangle(x, y, width, height, parent);
}
public function dispose():void
{
x = 0;
y = 0;
width = 0;
height = 0;
parent.pool.push(this);
}
public function IntRectangle(x:int = 0,y:int = 0,width:int=0,height:int=0, parent:IntRectManager = null)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.parent = parent;
}
}