Boolean32個とuint32bitどっちが速いか

by yasurageruheya
Booleanの方が管理しやすいよね
♥0 | Line 161 | Modified 2011-06-02 05:36:41 | 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/4fmQw
 */

package {
    import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.display.Graphics;
	import flash.utils.getTimer;
    public class FlashTest extends Sprite {
		private var txt:TextField;
		
		private var uintFlg:uint;
		
		private var flg0:Boolean;
		private var flg1:Boolean;
		private var flg2:Boolean;
		private var flg3:Boolean;
		private var flg4:Boolean;
		private var flg5:Boolean;
		private var flg6:Boolean;
		private var flg7:Boolean;
		private var flg8:Boolean;
		private var flg9:Boolean;
		private var flg10:Boolean;
		private var flg11:Boolean;
		private var flg12:Boolean;
		private var flg13:Boolean;
		private var flg14:Boolean;
		private var flg15:Boolean;
		private var flg16:Boolean;
		private var flg17:Boolean;
		private var flg18:Boolean;
		private var flg19:Boolean;
		private var flg20:Boolean;
		private var flg21:Boolean;
		private var flg22:Boolean;
		private var flg23:Boolean;
		private var flg24:Boolean;
		private var flg25:Boolean;
		private var flg26:Boolean;
		private var flg27:Boolean;
		private var flg28:Boolean;
		private var flg29:Boolean;
		private var flg30:Boolean;
		private var flg31:Boolean;
		
		private const COUNT:int = 10000000;
		
        public function FlashTest() {
            // write as3 code here..
            txt = new TextField();
			txt.autoSize = "left";
			addChild(txt);
			
			var spr:Sprite = createBtn("one more test", 0, 300);
			
			addChild(spr);
			
			spr.addEventListener(MouseEvent.CLICK, test);
			
			flg1 = flg3 = flg5 = flg7 = flg9 = flg11 = flg13 = flg15 = flg17 = flg19 = flg21 = flg23 = flg25 = flg27 = flg29 = flg31 = true;
			flg0 = flg2 = flg4 = flg6 = flg8 = flg10 = flg12 = flg14 = flg16 = flg18 = flg20 = flg22 = flg24 = flg26 = flg28 = flg30 = false;
			
			uintFlg = 0;
			var i:int = 32;
			while (i--)
			{
				uintFlg |= (this["flg" + i] ? 1 : 0) << i;
			}
			
			test(null);
        }
		
		private function test(e:MouseEvent):void 
		{
			txt.text = "uintFlg : " + uintFlg.toString(2);
			
			var i:int;
			var time:int;
			
			time = getTimer();
			i = COUNT;
			while (i--)
			{
				if (flg0) { }
				if (flg1) { }
				if (flg2) { }
				if (flg3) { }
				if (flg4) { }
				if (flg5) { }
				if (flg6) { }
				if (flg7) { }
				if (flg8) { }
				if (flg9) { }
				if (flg10) { }
				if (flg11) { }
				if (flg12) { }
				if (flg13) { }
				if (flg14) { }
				if (flg15) { }
				if (flg16) { }
				if (flg17) { }
				if (flg18) { }
				if (flg19) { }
				if (flg20) { }
				if (flg21) { }
				if (flg22) { }
				if (flg23) { }
				if (flg24) { }
				if (flg25) { }
				if (flg26) { }
				if (flg27) { }
				if (flg28) { }
				if (flg29) { }
				if (flg30) { }
				if (flg31) { }
			}
			
			txt.appendText("\nBoolean テスト : " + (getTimer() - time) + " mesc");
			
			time = getTimer();
			i = COUNT;
			
			while (i--)
			{
				if ((uintFlg >> 0) & 0x1) { }
				if ((uintFlg >> 1) & 0x1) { }
				if ((uintFlg >> 2) & 0x1) { }
				if ((uintFlg >> 3) & 0x1) { }
				if ((uintFlg >> 4) & 0x1) { }
				if ((uintFlg >> 5) & 0x1) { }
				if ((uintFlg >> 6) & 0x1) { }
				if ((uintFlg >> 7) & 0x1) { }
				if ((uintFlg >> 8) & 0x1) { }
				if ((uintFlg >> 9) & 0x1) { }
				if ((uintFlg >> 10) & 0x1) { }
				if ((uintFlg >> 11) & 0x1) { }
				if ((uintFlg >> 12) & 0x1) { }
				if ((uintFlg >> 13) & 0x1) { }
				if ((uintFlg >> 14) & 0x1) { }
				if ((uintFlg >> 15) & 0x1) { }
				if ((uintFlg >> 16) & 0x1) { }
				if ((uintFlg >> 17) & 0x1) { }
				if ((uintFlg >> 18) & 0x1) { }
				if ((uintFlg >> 19) & 0x1) { }
				if ((uintFlg >> 20) & 0x1) { }
				if ((uintFlg >> 21) & 0x1) { }
				if ((uintFlg >> 22) & 0x1) { }
				if ((uintFlg >> 23) & 0x1) { }
				if ((uintFlg >> 24) & 0x1) { }
				if ((uintFlg >> 25) & 0x1) { }
				if ((uintFlg >> 26) & 0x1) { }
				if ((uintFlg >> 27) & 0x1) { }
				if ((uintFlg >> 28) & 0x1) { }
				if ((uintFlg >> 29) & 0x1) { }
				if ((uintFlg >> 30) & 0x1) { }
				if ((uintFlg >> 31) & 0x1) { }
			}
			
			txt.appendText("\nuint テスト : " + (getTimer() - time) + " mesc");
		}
        
        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;
        }
    }
}