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

Initializing Vectors with fixed length

Initializing Vectors with fixed length as follows:

myVector.fixed = false;
myVector.length = 0;
myVector.length = nLength;
myVector.fixed = true;

長さを定めたVectorインスタンスを、上記のように初期化した。
Get Adobe Flash player
by Fumio 09 Nov 2011
    Embed
/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rmxY
 */

package {
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	[SWF(width = "240",height = "180")]
	public class InitializingVectors extends Sprite {
		private const MAX_NUMBER:int = 1000000;
		private var nLength:uint = 10;
		private var i:int;
		private var started:int;
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		public function InitializingVectors() {
			// Creating a TextField for display
			createTextField();
			// Starting test
			newOperator();
			lengthToZero();
		}
		private function newOperator():void {
			started = getTimer();
			for (i = 0; i < MAX_NUMBER; i++) {
				var myVector:Vector.<int> = new Vector.<int>(nLength, true);
				myVector[0] = 1;
			}
			xTrace("new", getTimer() - started);
		}
		private function lengthToZero():void {
			started = getTimer();
			var myVector:Vector.<int> = new Vector.<int>(nLength, true);
			for (i = 0; i < MAX_NUMBER; i++) {
				myVector.fixed = false;
				myVector.length = 0;
				myVector.length = nLength;
				myVector.fixed = true;
				myVector[0] = 1;
			}
			xTrace("Vector.length", getTimer() - started);
		}
		private function createTextField():void {
			addChild(my_txt);
			addChild(label_txt);
			my_txt.autoSize = TextFieldAutoSize.RIGHT;
			label_txt.autoSize = TextFieldAutoSize.LEFT;
		}
		private function xTrace(_str:String, n:int):void {
			my_txt.appendText(String(n) + "\n");
			label_txt.appendText(_str + ":" + "\n");
		}
	}
}