while アンロールで一番はやい書き方

by yasurageruheya
while1 が while(i--), while2 が while(--i > -1). while3 が while(--1 >= 0)
ウチの環境だと while1 が一番速く動いている気がするんだけれども、どれが一番どんな環境に対してでも速いのかな
♥0 | Line 65 | Modified 2014-09-02 00:58:26 | 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/9zva
 */

package {
	import flash.display.Sprite;
	import flash.events.Event;
	public class FlashTest extends Sprite {
		public const LOOP:int = 10000000;
		public const tester:Vector.<Tester> = new Vector.<Tester>();		
		public function FlashTest() {
			// write as3 code here..
			
			addEventListener(Event.ENTER_FRAME, onCountUp);
			tester[tester.length] = addChild(new Tester("while1", while1)) as Tester;
			tester[tester.length] = addChild(new Tester("while2", while2)) as Tester;
			tester[tester.length] = addChild(new Tester("while3", while3)) as Tester;
			
			var i:int = tester.length;
			while (i--) { tester[i].y = i * 20; }
			
			addEventListener(Event.ENTER_FRAME, onTest);
		}
		
		private function onTest(e:Event):void 
		{
			var i:int = tester.length;
			while (i--) { tester[i].test(); }
		}
		
		private function onCountUp(e:Event):void {++Tester.counter; }
		
		private function while1():void
		{
			var i:int = LOOP;
			while (i--) { }
		}
		
		private function while2():void
		{
			var i:int = LOOP;
			while (--i > -1) { }
		}
		
		private function while3():void
		{
			var i:int = LOOP;
			while (--i >= 0) { }
		}
	}
}
import flash.text.TextField;
import flash.utils.getTimer;

class Tester extends TextField
{
	public static var counter:int = 0;
	public static var loop:int = 1;
	public var totalTime:Number = 0;
	public var func:Function;
	public var funcName:String;
	
	public function test():void
	{
		var time:int = getTimer();
		var i:int = loop;
		while (--i > -1)
		{
			func();
		}
		totalTime += getTimer() - time;
		this.text = funcName + " average : " + (totalTime / counter) + ".ms";
	}
	
	public function Tester(funcName:String, func:Function)
	{
		this.funcName = funcName;
		this.func = func;
		this.autoSize = "left";
	}
}