flash on 2010-9-29

by John_Blackburne
...
@author
♥0 | Line 151 | Modified 2010-09-30 01:07:27 | MIT License
play

ActionScript3 source code

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

package 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author 
	 */
	public class Main extends Sprite 
	{
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			var txt:TextField = new TextField();
                        txt.autoSize = TextFieldAutoSize.LEFT;
                        txt.multiline = true;
			addChild(txt);
			var i:int, sum:int;
                        var aName:Vector.<String> = new Vector.<String>();
                        var aT0:Vector.<int> = new Vector.<int>();
                        var aT1:Vector.<int> = new Vector.<int>();
                        var aT2:Vector.<int> = new Vector.<int>();

                        aName.push("Array<Int>");
                        aT0.push(getTimer());
			var list1:Array = new Array(2000000, true);
			for (i = 0; i < 2000000; i++) list1[i] = i;
			
                        aT1.push(getTimer());
			//var sum:int = 0;  for each(i in list1) sum += i; //Ave = 96ms
		        sum = 0; for (i = 0; i < 2000000; i++) sum += list1[i]; //Ave = 390ms
                        aT2.push(getTimer());
			

                        aName.push("Vector<Int>");
                        aT0.push(getTimer());
			var list2:Vector.<int> = new Vector.<int>(2000000, true);
			for (i = 0; i < 2000000; i++) list2[i] = i;
			
		        aT1.push(getTimer());
			//var sum:int = 0; for each(i in list2) sum += i;  //Ave = 120ms
		        sum = 0; for (i = 0; i < 2000000; i++) sum += list2[i]; //Ave = 27ms
                        aT2.push(getTimer());
			
                        aName.push("Linked List of Int");
                        aT0.push(getTimer());
                        var list3:FastList_Int = new FastList_Int();
			for (i = 0; i < 2000000; i++) list3.add(i);
			
                        aT1.push(getTimer());
			sum = 0; var it:MixInteger = list3.head; while (it != null) { sum += it.elt; it = it.next; }
                        aT2.push(getTimer());

			
                        aName.push("UNTYPED linked list of Int");
                        aT0.push(getTimer());
			var list4:FastList = new FastList();
			for (i = 0; i < 2000000; i++) list4.add(i);
			
                        aT1.push(getTimer());
			sum = 0; var it1:FastNode = list4.head; while (it1 != null) { sum += it1.elt; it1 = it1.next; }
                        aT2.push(getTimer());
			
                        aName.push("Array<Integer>");
                        aT0.push(getTimer());
			var list5:Array = new Array(2000000, true);
			for (i = 0; i < 2000000; i++) list5[i] = new Integer(i);
			
                        aT1.push(getTimer());
			//var sum:int = 0; for each(var j:Integer in list) sum += j.elt; //Ave = 114ms
			sum = 0; for (i = 0; i < 2000000; i++) sum += list5[i].elt; //Ave = 746ms
                        aT2.push(getTimer());
			
                        aName.push("Vector<Integer>");
                        aT0.push(getTimer());
			var list6:Vector.<Integer> = new Vector.<Integer>(2000000);
			for (i = 0; i < 2000000; i++) list6[i] = new Integer(i);
			
                        aT1.push(getTimer());
			//var sum:int = 0; for each(var j:Integer in list6) sum += j.elt; //Ave = 125ms
			sum = 0; for (i = 0; i < 2000000; i++) sum += list6[i].elt; //Ave = 391ms
                        aT2.push(getTimer());
			
                        aName.push("Typed list of Integer");
                        aT0.push(getTimer());
			var list7:FastList_Integer = new FastList_Integer();
			for (i = 0; i < 2000000; i++) list7.add(new Integer(i));
			
                        aT1.push(getTimer());
			sum = 0; var it2:FastNode_Integer = list7.head;
			while (it2 != null) { sum += it2.elt; it2 = it2.next; }
                        aT2.push(getTimer());
			
                        aName.push("Inlined list of Integer");
                        aT0.push(getTimer());
			var list8:MixInteger = null;
			for (i = 0; i < 2000000; i++) list8 = new MixInteger(i, list8);
			
                        aT1.push(getTimer());
			sum = 0; var it3:MixInteger = list8; while (it3 != null) { sum += it3.elt; it3 = it3.next; }
                        aT2.push(getTimer());
			
                        aName.push("UNTYPED list of Integer");
                        aT0.push(getTimer());
			var list9:FastList = new FastList();
			for (i = 0; i < 2000000; i++) list9.add(new Integer(i));
			
                        aT1.push(getTimer());
		        sum = 0; var it4:FastNode = list9.head; while (it4 != null) { sum += it4.elt; it4 = it4.next; }
                        aT2.push(getTimer());
			
			/*~~~~~~~~~*/
                        
                        for (i = 0; i< aT0.length; i++) {
                            sum = aT1[i] - aT0[i];
                            txt.appendText(aName[i] + ":");
                            txt.appendText("{" + String(sum) + ", ");
                            sum = aT2[i] - aT1[i];
                            txt.appendText(String(sum) + "}\n");
                        }
		}
		
	}
	
}

class Integer {
	public var elt:int;
	public function Integer(i:int) {
		elt = i;
	}
}

class MixInteger {
	public var elt:int;
	public var next:MixInteger;
	public function MixInteger(i:int, n:MixInteger) {
		elt = i;
		next = n;
	}
}

class FastList_Int {
	public var head:MixInteger;
	public function FastList_Int() { }
	public function add(i:int):void {
		head = new MixInteger(i, head);
	}
}

class FastList_Integer {
	public var head:FastNode_Integer;
	public function FastList_Integer() { }
	public function add(i:Integer):void {
		head = new FastNode_Integer(i, head);
	}
}

class FastNode_Integer {
	public var elt:Integer;
	public var next:FastNode_Integer;
	public function FastNode_Integer(i:Integer, n:FastNode_Integer) {
		elt = i;
		next = n;
	}
}

class FastList {
	public var head:FastNode;
	public function FastList() { }
	public function add(i:*):void {
		head = new FastNode(i, head);
	}
}

class FastNode {
	public var elt:*;
	public var next:FastNode;
	public function FastNode(i:*, n:FastNode) {
		elt = i;
		next = n;
	}
}