var と const のアクセス速度差

by yasurageruheya
constの方が速度のブレが無い的な感じなのかな。 違いがよくわからない
♥0 | Line 68 | Modified 2013-10-26 18:20:10 | 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/oXFw
 */

package {
	import flash.display.Graphics;
	import flash.display.InteractiveObject;
    import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
    public class FlashTest extends Sprite {
		
		private var txt:TextField = new TextField();
		private const LOOP:int = 0xFFFFF;
		
		private var varTotal:uint = 0;
		private var constTotal:uint = 0;
		
		private var testCount:uint = 0;
		
        public function FlashTest() {
            // write as3 code here..
			var spr:Sprite = new Sprite();
			var t:TextField = new TextField();
			t.autoSize = "left";
			t.defaultTextFormat = new TextFormat(null, null, 0xFFFFFF);
			t.text = "クリックでテスト";
			t.selectable = false;
			spr.addChild(t);
			
			var grph:Graphics = spr.graphics;
			grph.beginFill(0x0);
			grph.drawRect(0, 0, t.width, t.height);
			grph.endFill();
			
			spr.buttonMode = true;
			
			addChild(spr);
			
            spr.addEventListener(MouseEvent.CLICK, testStart);
			
			txt.autoSize = "left";
			txt.y = spr.height;
			addChild(txt);
			
			
			spr.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
        }
		
		private function testStart(e:MouseEvent):void
		{
			var io:InteractiveObject = e.currentTarget as InteractiveObject;
			io.removeEventListener(MouseEvent.CLICK, testStart);
			
			testCount++;
			
			var varVal:int = 10;
			var msec:int;
			const constVal:int = 10;
			
			txt.text = "";
			
			
			var tmp:int;
			
			var i:int = LOOP;
			var time:int = getTimer();
			while (i--)
			{
				tmp = varVal;
			}
			msec = getTimer() - time;
			txt.appendText("\nvar テスト : " + msec.toString() + "msec.");
			
			varTotal += msec;
			
			
			i = LOOP;
			time = getTimer();
			while (i--)
			{
				tmp = constVal;
			}
			msec = getTimer() - time;
			txt.appendText("\nconst テスト : " + msec.toString() + "msec.");
			
			constTotal += msec;
			
			txt.appendText("\nvarテスト平均 : " + (varTotal / testCount) + "msec.");
			
			txt.appendText("\nconstテスト平均 : " + (constTotal / testCount) + "msec.");
			
			io.addEventListener(MouseEvent.CLICK, testStart);
		}
    }
}