flash on 2009-9-22 hika forked:0favorite:0lines:57license : MIT License modified : 2009-09-22 19:41:05 Embed Tweet package { import flash.display.*; import flash.events.*; import flash.utils.*; import flash.text.*; [SWF(backgroundColor="#ffffff", frameRate="60", width="100", height="100")] public class testAutoPool extends Sprite{ public function testAutoPool() { addEventListener( Event.ADDED_TO_STAGE, ADDED_TO_STAGE ); } public function ADDED_TO_STAGE( $e:Event ): void { removeEventListener( Event.ADDED_TO_STAGE, ADDED_TO_STAGE ); stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; var pool:BitmapAutoPool = new BitmapAutoPool; var bitmap:Bitmap, i:uint, a:uint; addChild( new TextField ); //풀링을 사용한 경우 a = getTimer(); for( i = 0 ; i < 100000 ; ++i ){ this.addChild( pool.getBitmap() ); this.removeChild( this.getChildAt(1) ); } TextField(getChildAt(0)).htmlText = ( getTimer() - a ).toString(); //풀링을 사용하지 않는 경우 a = getTimer(); for( i = 0 ; i < 100000 ; ++i ){ this.addChild( new Bitmap ); this.removeChild( this.getChildAt(1) ); } TextField(getChildAt(0)).htmlText += ':' + ( getTimer() - a ).toString(); } } } import flash.display.*; class BitmapAutoPool{ private var _pool:Vector.<Bitmap>; public function BitmapAutoPool(){ //적당히 수로 초기화한다. _pool = new Vector.<Bitmap>(); } public function getBitmap():Bitmap{ var key:*, result:Bitmap; //먼저 기존의 pool에서 찾아본다. for( key in _pool){ //만약 해당 객체가 null이라면 new를 통해 생성한다. if( _pool[key] === null ){ _pool[key] = new Bitmap; result = _pool[key]; break; //null이 아니라면 parent를 조사하여 사용가능한지 판단한다. }else if( _pool[key].parent === null ){ result = _pool[key]; break; } } //기존의 pool안에서 쓸만한 걸 찾지 못했다면 if( result === null ){ //새로운 인스턴스를 만들고 풀에 추가한다. result = new Bitmap; _pool[_pool.length] = result; } //인스턴스를 반환한다. return result; } } Code Fullscreen Preview Fullscreen ActivityEvent.ADDED_TO_STAGE getTimer getChildAt htmlText Bitmap removeChild toString Event.ADDED_TO_STAGE removeEventListener StageAlign.TOP_LEFT align scaleMode parent StageScaleMode.NO_SCALE TextField addEventListener addChild length Vector Event