Decrement versus Increment in Loop

by Fumio
♥0 | Line 43 | Modified 2010-05-03 17:29:19 | MIT License
play

ActionScript3 source code

/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/pB5I
 */

package {
	import flash.display.Sprite;
	import flash.utils.getTimer;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	public class Decrement_vs_Increment extends Sprite {
		private static var my_txt:TextField = new TextField();
		private const MAX_NUMBER:int = 500000000;
		private var i:int;
		private var started:int;
		public function Decrement_vs_Increment() {
			addChild(my_txt);
			my_txt.autoSize = TextFieldAutoSize.LEFT;
			testDecrement();
			testIncrement();
			testDecrementPost();
		}
		private function testDecrement():void {
			i = MAX_NUMBER;
			started = getTimer();
			while (--i > -1) {
			}
			xTrace(getTimer() - started);
		}
		private function testIncrement():void {
			started = getTimer();
			while (++i < MAX_NUMBER) {
			}
			xTrace(getTimer() - started);
		}
		private function testDecrementPost():void {
			i = MAX_NUMBER;
			started = getTimer();
			while (i-- > 0) {
			}
			xTrace(getTimer() - started);
		}
		private static function xTrace(n:int):void {
			var trace_str:String = String(n);
			my_txt.appendText(trace_str + "\n");
			// trace(trace_str);
		}
	}
}

Forked