インデックス番号が知りたい時

by yasurageruheya
♥0 | Line 176 | Modified 2012-02-22 03:10:06 | MIT License
play

ActionScript3 source code

/**
 * Copyright yasurageruheya ( http://wonderfl.net/user/yasurageruheya )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dveW
 */

package {
    import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
    public class FlashTest extends Sprite {
		
		private const TEST_COUNT:int = 100000;
		
		private const KEY:String = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ";
		
		private const txt:TextField = new TextField();
		
        public function FlashTest() {
            // write as3 code here..
			txt.autoSize = "left";
			
            if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
        }
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			addChild(txt);
			test();
		}
		
		private function test(e:MouseEvent = null):void 
		{
			stage.removeEventListener(MouseEvent.CLICK, test);
			
			const vec:Vector.<String> = Vector.<String>(KEY.split(""));
			const obj:Object = { };
			var i:int = KEY.length;
			while (i--)
			{
				obj[KEY.charAt(i)] = i;
			}
			var timer:int;
			var j:int;
			const len:int = vec.length;
			var idx:int;
			const a:String = "a";
			const Z:String = "Z";
			const R:String = "R";
			txt.text = vec.toString() + "\n";
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				j = len;
				while (j--)
				{
					if (vec[j] == Z)
					{
						idx = j;
						break;
					}
				}
			}
			txt.appendText("アンロール1 Z探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				j = vec.length;
				while (j--)
				{
					if (vec[j] == Z)
					{
						idx = j;
						break;
					}
				}
			}
			txt.appendText("アンロール2 Z探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				idx = vec.lastIndexOf(Z);
			}
			txt.appendText("lastIndexOf Z探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				idx = obj[Z];
			}
			txt.appendText("Object.Z : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				j = len;
				while (j--)
				{
					if (vec[j] == a)
					{
						idx = j;
						break;
					}
				}
			}
			txt.appendText("アンロール1 a探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				j = vec.length;
				while (j--)
				{
					if (vec[j] == a)
					{
						idx = j;
						break;
					}
				}
			}
			txt.appendText("アンロール2 a探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				idx = vec.lastIndexOf(a);
			}
			txt.appendText("lastIndexOf a探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				idx = obj[a];
			}
			txt.appendText("Object.a : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				j = len;
				while (j--)
				{
					if (vec[j] == R)
					{
						idx = j;
						break;
					}
				}
			}
			txt.appendText("アンロール1 R探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				j = vec.length;
				while (j--)
				{
					if (vec[j] == R)
					{
						idx = j;
						break;
					}
				}
			}
			txt.appendText("アンロール2 R探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				idx = vec.lastIndexOf(R);
			}
			txt.appendText("lastIndexOf R探査 : " + (getTimer() - timer) + "msec.\n");
			
			i = TEST_COUNT;
			timer = getTimer();
			while (i--)
			{
				idx = obj[R];
			}
			txt.appendText("Object.R : " + (getTimer() - timer) + "msec.\n");
			
			txt.appendText("クリックで再計算");
			
			stage.addEventListener(MouseEvent.CLICK, test);
		}
    }
}