静的定数へのアクセスは遅いらしい

by yasurageruheya
これは気にするべき数字なのかしら
うーん・・・。よく分からない・・・
♥0 | Line 107 | Modified 2011-06-02 18:57:53 | 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/1G7g
 */

package {
    import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	import flash.display.Graphics;
    public class FlashTest extends Sprite {
		private const MAX_VALUE:int = int.MAX_VALUE;
                private static const MAX_VAL:int = int.MAX_VALUE;
		private const PI:Number = Math.PI;
		private const COUNT:int = 20000000;
		private var txt:TextField;
		private var cst:Constant;
        public function FlashTest() {
            // write as3 code here..
            
			txt = new TextField();
			txt.autoSize = "left";
			addChild(txt);
			
			cst = new Constant();
			
			var spr:Sprite = createBtn("one mone test", 0, 400);
			
			spr.addEventListener(MouseEvent.CLICK, test);
			
			test(null);
        }
		
		private function test(e:MouseEvent):void 
		{
			var time:int;
			var i:int;
			var n:int;
			var num:Number;
			
			txt.text = "テスト : " + COUNT + "回アクセス\n===============\n";
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				n = MAX_VALUE;
			}
			
			txt.appendText("private const MAX_VALUE にアクセス("+MAX_VALUE+")\n" + (getTimer() - time) + " msec\n===============\n");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				n = MAX_VAL;
			}
			
			txt.appendText("private static const MAX_VAL にアクセス("+MAX_VALUE+")\n" + (getTimer() - time) + " msec\n===============\n");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				n = int.MAX_VALUE;
			}
			
			txt.appendText("int.MAX_VALUE にアクセス("+int.MAX_VALUE+")\n" + (getTimer() - time) + " msec\n===============\n");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				n = Constant.MAX_VALUE;
			}
			
			txt.appendText("Constant(public static const).MAX_VALUE にアクセス("+Constant.MAX_VALUE+")\n" + (getTimer() - time) + " msec\n===============\n");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				num = cst.PI;
			}
			
			txt.appendText("Constant(public const).PI にアクセス("+cst.PI+")\n" + (getTimer() - time) + " msec\n===============\n");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				num = Math.PI;
			}
			
			txt.appendText("Math.PI にアクセス("+Math.PI+")\n" + (getTimer() - time) + " msec\n===============\n");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				num = PI;
			}
			
			txt.appendText("private const PI にアクセス("+PI+")\n" + (getTimer() - time) + " msec\n===============\n");
		}
        
        private function createBtn(str:String, x:int, y:int):Sprite
        {
            var txt:TextField = new TextField();
            var spr:Sprite = new Sprite();
            txt.autoSize = "left";
            txt.textColor = 0xFFFFFF;
            txt.text = str;
            txt.selectable = false;
            spr.addChild(txt);
            var g:Graphics = spr.graphics;
            g.beginFill(0, 1);
            g.drawRect(0, 0, spr.width, spr.height);
            g.endFill();
            
            spr.x = x;
            spr.y = y;
            
            addChild(spr);
            
            return spr;
        }
    }
}

class Constant
{
	public static const MAX_VALUE:int = int.MAX_VALUE;
	public const PI:Number = Math.PI;
	
	public function Constant()
	{
		
	}
}