Loop Benchmar Test 3: Linked List vs. Event
♥0 |
Line 106 |
Modified 2010-05-19 01:00:49 |
MIT License
archived:2017-03-10 19:15:12
ActionScript3 source code
/**
* Copyright jozefchutka ( http://wonderfl.net/user/jozefchutka )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/wmTu
*/
package
{
import com.bit101.components.HBox;
import com.bit101.components.PushButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
[SWF(width="465", height="465", frameRate="30", backgroundColor="#FFFFFF")]
public class WonderflApp extends Sprite
{
private var firstItem:Item;
private var logTextField:TextField = new TextField();
private static const R:uint = 500000;
public function WonderflApp():void
{
logTextField.y = 20;
logTextField.width = stage.stageWidth;
logTextField.height = stage.stageHeight - logTextField.y;
addChild(logTextField);
var box:HBox = new HBox(this);
box.spacing = 0;
new PushButton(box, 0, 0, "Linked List", testLinked).width = 60;
new PushButton(box, 0, 0, "Events", testEvents).width = 60;
}
private function log(message:String):void
{
logTextField.appendText(message + "\n");
logTextField.scrollV = logTextField.maxScrollV;
}
private function measure(methodName:String):void
{
var method:Function = this[methodName];
var t0:Date = new Date();
method();
var t:uint = new Date().time - t0.time;
log(methodName + ": " + t + " ms.");
}
private function generateItems():void
{
if(firstItem)
return;
log("----- Generating items -----");
var item:Item = new Item(this);
var pItem:Item;
pItem = firstItem = item;
for(var i:uint = 1; i < R; i++)
{
item = new Item(this);
pItem.next = item;
pItem = item;
}
}
private function testLinked(... rest):void
{
generateItems();
log("--- Starting Linked List test --- (" + R +" iterations)");
measure("testLinkedExe");
log("");
}
public function testLinkedExe():void
{
var item:Item = firstItem;
var count:uint;
while(item)
{
//count++;
item = item.next;
}
}
private function testEvents(... rest):void
{
generateItems();
log("--- Starting Events test --- (" + R +" iterations)");
measure("testEventsExe");
log("");
}
private function testEventsExe():void
{
var event:ItemEvent = new ItemEvent(ItemEvent.COUNT);
dispatchEvent(event);
}
}
}
import flash.events.EventDispatcher;
import flash.events.Event;
class Item
{
public var next:Item;
public function Item(dispatcher:EventDispatcher)
{
dispatcher.addEventListener(ItemEvent.COUNT, count);
}
private function count(event:ItemEvent):void
{
//event.count++;
}
}
class ItemEvent extends Event
{
public static const COUNT:String = "ItemEventCOUNT";
public static const FIND:String = "ItemEventFIND";
public var count:uint = 0;
public function ItemEvent(type:String, bubbles:Boolean=false,
cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
}
}