flash on 2009-9-22
♥0 |
Line 57 |
Modified 2009-09-22 19:41:05 |
MIT License
archived:2017-03-10 10:02:59
ActionScript3 source code
/**
* Copyright hika ( http://wonderfl.net/user/hika )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/4Itz
*/
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;
}
}