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

Setting a computed number to an element of Array 2

Get Adobe Flash player
by Fumio 17 Feb 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/eTlU
 */

package {
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	[SWF(width = "240",height = "180")]
	public class IndexOfElement extends Sprite {
		private const LENGTH:uint = 2000000;
		private const AMOUNT:uint = LENGTH * 2;
		private var my_array:Array = [];
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		private var my_fmt:TextFormat = new TextFormat();
		public function IndexOfElement() {
			// Creating a TextField for display
			createTextField();
			createArray();
			warmingUp();
			// Starting Test
			putDirectly();
			castAndPut();
			assignToVariable();
			assignToVariable2();
			assignToVariable3();
		}
		private function warmingUp():void {
			var _array:Array = my_array;
			// var nLength:uint = LENGTH;
			var nAmount:uint = AMOUNT;
			var even_array:Array = [];
			var odd_array:Array = [];
			var started:int = getTimer();
			for (var i:uint = 0; i < nAmount; i++) {
				even_array[i] = my_array[i];
				odd_array[i] = my_array[i];
			}
		}
		private function putDirectly():void {
			var _array:Array = my_array;
			var nLength:uint = LENGTH;
			var even_array:Array = [];
			var odd_array:Array = [];
			var started:int = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				even_array[i] = my_array[i * 2];
				odd_array[i] = my_array[i * 2 + 1];
			}
			xTrace("[i * 2]",getTimer() - started);
		}
		private function castAndPut():void {
			var _array:Array = my_array;
			var nLength:uint = LENGTH;
			var even_array:Array = [];
			var odd_array:Array = [];
			var started:int = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				even_array[i] = my_array[uint(i * 2)];
				odd_array[i] = my_array[uint(i * 2 + 1)];
			}
			xTrace("[uint(i * 2)]",getTimer() - started);
		}
		private function assignToVariable():void {
			var _array:Array = my_array;
			var nLength:uint = LENGTH;
			var even_array:Array = [];
			var odd_array:Array = [];
			var started:int = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				var n:uint = i * 2;
				// var m:uint = i * 2 + 1;
				var m:uint = n + 1;
				even_array[i] = my_array[n];
				odd_array[i] = my_array[m];
			}
			xTrace("var n:uint = i * 2",getTimer() - started);
		}
		private function assignToVariable2():void {
			var _array:Array = my_array;
			var nLength:uint = LENGTH;
			var even_array:Array = [];
			var odd_array:Array = [];
			var started:int = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				var n:uint = i * 2;
				even_array[i] = my_array[n];
				odd_array[i] = my_array[++n];
			}
			xTrace("var n:uint = i * 2 and ++",getTimer() - started);
		}
		private function assignToVariable3():void {
			var _array:Array = my_array;
			var nLength:uint = LENGTH;
			var even_array:Array = [];
			var odd_array:Array = [];
			var started:int = getTimer();
			for (var i:uint = 0; i < nLength; i++) {
				var n:uint = i * 2;
				even_array[i] = my_array[n];
				++n;
				odd_array[i] = my_array[n];
			}
			xTrace("var n:uint = i * 2 and ++",getTimer() - started);
		}
		private function createArray():void {
			var nAmount:uint = AMOUNT;
			for (var i:uint = 0; i < nAmount; i++) {
				my_array[i] = i;
			}
		}
		private function createTextField():void {
			addChild(my_txt);
			addChild(label_txt);
			my_fmt.align = TextFormatAlign.RIGHT;
			my_txt.x +=  50;
			my_txt.defaultTextFormat = my_fmt;
			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");
		}
	}
}