forked from: if 文と switch 文の条件比較回数とか

by yasurageruheya forked from if 文と switch 文の条件比較回数とか (diff: 880)
http://wonderfl.net/c/wTL7 のテスト用のコードを関数化したら10倍くらい高速化したので、ループ回数を10倍くらい増やしました。 あと条件に一致しない「500」での比較も追加。
♥0 | Line 511 | Modified 2015-10-28 00:03:23 | 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/0Ktv
 */

// forked from yasurageruheya's if 文と switch 文の条件比較回数とか
package {
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	public class FlashTest extends Sprite {
		
		public const LOOP:int = 120000;
		
		public const txt:TextField = new TextField();
		
		public var tests:Vector.<Tester> = new Vector.<Tester>();
		
		public var freeMemory:int;
		
		public var gcCount10byte:int = 0;
		
		public var gcCount100byte:int = 0;
		
		public var gcCount1KB:int = 0;
		
		public var gcCount10KB:int = 0;
		
		public var gcCount100KB:int = 0
		
		public var gcCount1MB:int = 0;
		
		public var gcCount10MB:int = 0;
		
		public var gcCount100MB:int = 0;
		
		public var gcCount:int = 0;
		
		public var totalFrames:Number = 1;
		
		public function FlashTest() {
			
			
			tests[tests.length] = new Tester("if === target = 0\t: ", function():void
			{
				ifTest1(0);
			});
			
			tests[tests.length] = new Tester("if === target = 50\t: ", function():void
			{
				ifTest1(50);
			});
			
			tests[tests.length] = new Tester("if === target = 99\t: ", function():void
			{
				ifTest1(99);
			});
			
			tests[tests.length] = new Tester("if === target = 500\t: ", function():void
			{
				ifTest1(500);
			});
			
			
			tests[tests.length] = new Tester("if == target = 0\t: ", function():void
			{
				ifTest2(0);
			});
			
			tests[tests.length] = new Tester("if == target = 50\t: ", function():void
			{
				ifTest2(50);
			});
			
			tests[tests.length] = new Tester("if == target = 99\t: ", function():void
			{
				ifTest2(99);
			});
			
			tests[tests.length] = new Tester("if == target = 500\t: ", function():void
			{
				ifTest2(500);
			});
			
			
			tests[tests.length] = new Tester("switch target = 0\t: ", function():void
			{
				switchTest(0);
			});
			
			tests[tests.length] = new Tester("switch target = 50\t: ", function():void
			{
				switchTest(50);
			});
			
			tests[tests.length] = new Tester("switch target = 99\t: ", function():void
			{
				switchTest(99);
			});
			
			tests[tests.length] = new Tester("switch target = 500\t: ", function():void
			{
				switchTest(500);
			});
			
			tests.reverse();
			var format:TextFormat = txt.getTextFormat();
			format.font = "_等幅";
			txt.defaultTextFormat = format;
			txt.width = 500;
			txt.height = 500;
			addChild(txt);
			
			freeMemory = System.freeMemory;
			
			// write as3 code here..
			addEventListener(Event.ENTER_FRAME, test);
		}
		
		private function test(e:Event):void 
		{
			var str:String = "";
			var i:int = tests.length;
			while (i--)
			{
				str += tests[i].start();
			}
			str += "\nmemory : total " + System.totalMemory + " / private " + System.privateMemory + "\n";
			
			const currentFreeMemory:Number = System.freeMemory;
			
			if (freeMemory < currentFreeMemory)
			{
				gcCount++;
				const gcMemory:Number = currentFreeMemory - freeMemory;
				if (gcMemory > 100000000)
				{
					gcCount100MB++;
				}
				else if (gcMemory > 10000000)
				{
					gcCount10MB++;
				}
				else if (gcMemory > 1000000)
				{
					gcCount1MB++;
				}
				else if (gcMemory > 100000)
				{
					gcCount100KB++;
				}
				else if (gcMemory > 10000)
				{
					gcCount10KB++;
				}
				else if (gcMemory > 1000)
				{
					gcCount1KB++;
				}
				else if (gcMemory > 100)
				{
					gcCount100byte++;
				}
				else if (gcMemory > 10)
				{
					gcCount10byte++;
				}
			}
			freeMemory = System.freeMemory;
			
			str += "\n100MB\tGC発生回数?\t: " + gcCount100MB;
			str += "\n10MB\tGC発生回数?\t: " + gcCount10MB;
			str += "\n1MB\t\tGC発生回数?\t: " + gcCount1MB;
			str += "\n100KB\tGC発生回数?\t: " + gcCount100KB;
			str += "\n10KB\tGC発生回数?\t: " + gcCount10KB;
			str += "\n1KB\t\tGC発生回数?\t: " + gcCount1KB;
			str += "\n100byte\tGC発生回数?\t: " + gcCount100byte;
			str += "\n10byte\tGC発生回数?\t: " + gcCount10byte;
			str += "\n\t\t計GC発生回数?\t: " + gcCount;
			
			str += "\n\t\tGC発生率?\t\t: " + ((gcCount / (totalFrames++)) * 100) + "%";
			
			txt.text = str;
		}
		
		[Inline]
		final private function ifTest1(target:int):void
		{
			var i:int = LOOP;
			const TARGET:int = target;
			while (i--)
			{
				if (TARGET === 0) { }
				else if (TARGET === 1) { }
				else if	(TARGET === 2) { }
				else if	(TARGET === 3) { }
				else if	(TARGET === 4) { }
				else if	(TARGET === 5) { }
				else if	(TARGET === 6) { }
				else if	(TARGET === 7) { }
				else if	(TARGET === 8) { }
				else if	(TARGET === 9) { }
				else if	(TARGET === 10) { }
				else if	(TARGET === 11) { }
				else if	(TARGET === 12) { }
				else if	(TARGET === 13) { }
				else if	(TARGET === 14) { }
				else if	(TARGET === 15) { }
				else if	(TARGET === 16) { }
				else if	(TARGET === 17) { }
				else if	(TARGET === 18) { }
				else if	(TARGET === 19) { }
				else if	(TARGET === 20) { }
				else if	(TARGET === 21) { }
				else if	(TARGET === 22) { }
				else if	(TARGET === 23) { }
				else if	(TARGET === 24) { }
				else if	(TARGET === 25) { }
				else if	(TARGET === 26) { }
				else if	(TARGET === 27) { }
				else if	(TARGET === 28) { }
				else if	(TARGET === 29) { }
				else if	(TARGET === 30) { }
				else if	(TARGET === 31) { }
				else if	(TARGET === 32) { }
				else if	(TARGET === 33) { }
				else if	(TARGET === 34) { }
				else if	(TARGET === 35) { }
				else if	(TARGET === 36) { }
				else if	(TARGET === 37) { }
				else if	(TARGET === 38) { }
				else if	(TARGET === 39) { }
				else if	(TARGET === 40) { }
				else if	(TARGET === 41) { }
				else if	(TARGET === 42) { }
				else if	(TARGET === 43) { }
				else if	(TARGET === 44) { }
				else if	(TARGET === 45) { }
				else if	(TARGET === 46) { }
				else if	(TARGET === 47) { }
				else if	(TARGET === 48) { }
				else if	(TARGET === 49) { }
				else if	(TARGET === 50) { }
				else if	(TARGET === 51) { }
				else if	(TARGET === 52) { }
				else if	(TARGET === 53) { }
				else if	(TARGET === 54) { }
				else if	(TARGET === 55) { }
				else if	(TARGET === 55) { }
				else if	(TARGET === 56) { }
				else if	(TARGET === 57) { }
				else if	(TARGET === 58) { }
				else if	(TARGET === 59) { }
				else if	(TARGET === 60) { }
				else if	(TARGET === 61) { }
				else if	(TARGET === 62) { }
				else if	(TARGET === 63) { }
				else if	(TARGET === 64) { }
				else if	(TARGET === 65) { }
				else if	(TARGET === 66) { }
				else if	(TARGET === 67) { }
				else if	(TARGET === 68) { }
				else if	(TARGET === 69) { }
				else if	(TARGET === 70) { }
				else if	(TARGET === 71) { }
				else if	(TARGET === 72) { }
				else if	(TARGET === 73) { }
				else if	(TARGET === 74) { }
				else if	(TARGET === 75) { }
				else if	(TARGET === 76) { }
				else if	(TARGET === 77) { }
				else if	(TARGET === 78) { }
				else if	(TARGET === 79) { }
				else if	(TARGET === 80) { }
				else if	(TARGET === 81) { }
				else if	(TARGET === 82) { }
				else if	(TARGET === 83) { }
				else if	(TARGET === 84) { }
				else if	(TARGET === 85) { }
				else if	(TARGET === 86) { }
				else if	(TARGET === 87) { }
				else if	(TARGET === 88) { }
				else if	(TARGET === 89) { }
				else if	(TARGET === 90) { }
				else if	(TARGET === 91) { }
				else if	(TARGET === 92) { }
				else if	(TARGET === 93) { }
				else if	(TARGET === 94) { }
				else if	(TARGET === 95) { }
				else if	(TARGET === 96) { }
				else if	(TARGET === 97) { }
				else if	(TARGET === 98) { }
				else if	(TARGET === 99) { }
				else {}
			}
		}
		
		[Inline]
		final private function ifTest2(target:int):void
		{
			var i:int = LOOP;
			const TARGET:int = target;
			while (i--)
			{
				if (TARGET == 0) { }
				else if (TARGET == 1) { }
				else if	(TARGET == 2) { }
				else if	(TARGET == 3) { }
				else if	(TARGET == 4) { }
				else if	(TARGET == 5) { }
				else if	(TARGET == 6) { }
				else if	(TARGET == 7) { }
				else if	(TARGET == 8) { }
				else if	(TARGET == 9) { }
				else if	(TARGET == 10) { }
				else if	(TARGET == 11) { }
				else if	(TARGET == 12) { }
				else if	(TARGET == 13) { }
				else if	(TARGET == 14) { }
				else if	(TARGET == 15) { }
				else if	(TARGET == 16) { }
				else if	(TARGET == 17) { }
				else if	(TARGET == 18) { }
				else if	(TARGET == 19) { }
				else if	(TARGET == 20) { }
				else if	(TARGET == 21) { }
				else if	(TARGET == 22) { }
				else if	(TARGET == 23) { }
				else if	(TARGET == 24) { }
				else if	(TARGET == 25) { }
				else if	(TARGET == 26) { }
				else if	(TARGET == 27) { }
				else if	(TARGET == 28) { }
				else if	(TARGET == 29) { }
				else if	(TARGET == 30) { }
				else if	(TARGET == 31) { }
				else if	(TARGET == 32) { }
				else if	(TARGET == 33) { }
				else if	(TARGET == 34) { }
				else if	(TARGET == 35) { }
				else if	(TARGET == 36) { }
				else if	(TARGET == 37) { }
				else if	(TARGET == 38) { }
				else if	(TARGET == 39) { }
				else if	(TARGET == 40) { }
				else if	(TARGET == 41) { }
				else if	(TARGET == 42) { }
				else if	(TARGET == 43) { }
				else if	(TARGET == 44) { }
				else if	(TARGET == 45) { }
				else if	(TARGET == 46) { }
				else if	(TARGET == 47) { }
				else if	(TARGET == 48) { }
				else if	(TARGET == 49) { }
				else if	(TARGET == 50) { }
				else if	(TARGET == 51) { }
				else if	(TARGET == 52) { }
				else if	(TARGET == 53) { }
				else if	(TARGET == 54) { }
				else if	(TARGET == 55) { }
				else if	(TARGET == 55) { }
				else if	(TARGET == 56) { }
				else if	(TARGET == 57) { }
				else if	(TARGET == 58) { }
				else if	(TARGET == 59) { }
				else if	(TARGET == 60) { }
				else if	(TARGET == 61) { }
				else if	(TARGET == 62) { }
				else if	(TARGET == 63) { }
				else if	(TARGET == 64) { }
				else if	(TARGET == 65) { }
				else if	(TARGET == 66) { }
				else if	(TARGET == 67) { }
				else if	(TARGET == 68) { }
				else if	(TARGET == 69) { }
				else if	(TARGET == 70) { }
				else if	(TARGET == 71) { }
				else if	(TARGET == 72) { }
				else if	(TARGET == 73) { }
				else if	(TARGET == 74) { }
				else if	(TARGET == 75) { }
				else if	(TARGET == 76) { }
				else if	(TARGET == 77) { }
				else if	(TARGET == 78) { }
				else if	(TARGET == 79) { }
				else if	(TARGET == 80) { }
				else if	(TARGET == 81) { }
				else if	(TARGET == 82) { }
				else if	(TARGET == 83) { }
				else if	(TARGET == 84) { }
				else if	(TARGET == 85) { }
				else if	(TARGET == 86) { }
				else if	(TARGET == 87) { }
				else if	(TARGET == 88) { }
				else if	(TARGET == 89) { }
				else if	(TARGET == 90) { }
				else if	(TARGET == 91) { }
				else if	(TARGET == 92) { }
				else if	(TARGET == 93) { }
				else if	(TARGET == 94) { }
				else if	(TARGET == 95) { }
				else if	(TARGET == 96) { }
				else if	(TARGET == 97) { }
				else if	(TARGET == 98) { }
				else if	(TARGET == 99) { }
				else {}
			}
		}
		
		[Inline]
		final private function switchTest(target:int):void
		{
			var i:int = LOOP;
			const TARGET:int = target;
			while (i--)
			{
				switch(TARGET)
				{
					case 0: break;
					case 1: break;
					case 2: break;
					case 3: break;
					case 4: break;
					case 5: break;
					case 6: break;
					case 7: break;
					case 8: break;
					case 9: break;
					case 10: break;
					case 11: break;
					case 12: break;
					case 13: break;
					case 14: break;
					case 15: break;
					case 16: break;
					case 17: break;
					case 18: break;
					case 19: break;
					case 20: break;
					case 21: break;
					case 22: break;
					case 23: break;
					case 24: break;
					case 25: break;
					case 26: break;
					case 27: break;
					case 28: break;
					case 29: break;
					case 30: break;
					case 31: break;
					case 32: break;
					case 33: break;
					case 34: break;
					case 35: break;
					case 36: break;
					case 37: break;
					case 38: break;
					case 39: break;
					case 40: break;
					case 41: break;
					case 42: break;
					case 43: break;
					case 44: break;
					case 45: break;
					case 46: break;
					case 47: break;
					case 48: break;
					case 49: break;
					case 50: break;
					case 51: break;
					case 52: break;
					case 53: break;
					case 54: break;
					case 55: break;
					case 55: break;
					case 56: break;
					case 57: break;
					case 58: break;
					case 59: break;
					case 60: break;
					case 61: break;
					case 62: break;
					case 63: break;
					case 64: break;
					case 65: break;
					case 66: break;
					case 67: break;
					case 68: break;
					case 69: break;
					case 70: break;
					case 71: break;
					case 72: break;
					case 73: break;
					case 74: break;
					case 75: break;
					case 76: break;
					case 77: break;
					case 78: break;
					case 79: break;
					case 80: break;
					case 81: break;
					case 82: break;
					case 83: break;
					case 84: break;
					case 85: break;
					case 86: break;
					case 87: break;
					case 88: break;
					case 89: break;
					case 90: break;
					case 91: break;
					case 92: break;
					case 93: break;
					case 94: break;
					case 95: break;
					case 96: break;
					case 97: break;
					case 98: break;
					case 99: break;
					default:
				}
			}
		}
	}
}

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.geom.Rectangle;
import flash.system.System;
import flash.utils.ByteArray;
import flash.utils.Dictionary;
import flash.utils.getTimer;
class Tester
{
	public var time:Number = 0;
	
	public var name:String;
	
	public var test:Function;
	
	public var count:Number = 1;
	
	public function start():String
	{
		const startTime:int = getTimer();
		test();
		time += getTimer() - startTime;
		return name + (time / (count++)) + " ms.\n";
	}
	
	public function get timeAverage():Number
	{
		return time / count;
	}
	
	public function Tester(name:String, test:Function)
	{
		this.name = name;
		this.test = test;
	}
}