In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Single vs Double Loop

Get Adobe Flash player
by Fumio 15 Jul 2010
package {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	public class FlashTest extends Sprite {
		public var _txt:TextField;
		public var sprite0:Sprite;
		public var sprite1:Sprite;
		private var n:int = 10000;
		public function FlashTest() {
			createTestEnvironment();
			loopTest();
		}
		private function createTestEnvironment():void {
			_txt = new TextField();
			_txt.autoSize = TextFieldAutoSize.LEFT;
			_txt.wordWrap = true;
			_txt.width = stage.stageWidth;
			addChild(_txt);
			sprite0 = new Sprite();
			addChild(sprite0);
			sprite1 = new Sprite();
			addChild(sprite1);
		}
		private function loopTest():void {
			var t0:int = loop0();
			var t1:int = loop1();
			var _str:String = "";
			_str += "0: " + t0 + ", ";
			_str+="1: "+t1+", ";
			_str+="ratio: "+t0/t1;
			_txt.text=_str;
		}
		private function loop0():int {
			var t:int=getTimer();
			for (var i:int=0; i<n; i++) {
				var mySprite:Sprite = new Sprite();
				sprite0.addChild(mySprite);
			}
			return getTimer() - t;
		}
		private function loop1():int {
			var t:int=getTimer();
			var _array:Array = new Array();
			var i:int;
			for (i=0; i<n; i++) {
				// var mySprite:Sprite = new Sprite();
				_array[i] = new Sprite();
			}
			for (i=0; i<n; i++) {
				var mySprite:Sprite=_array[i];
				sprite1.addChild(mySprite);
			}
			return getTimer() - t;
		}
	}
}