Skip to content

Instantly share code, notes, and snippets.

@IQAndreas

IQAndreas/Main.as

Created Jan 17, 2012
Embed
What would you like to do?
Performance test for Static Vs Non-Static
package
{
import flash.utils.getTimer;
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.Event;
public class Main extends Sprite
{
public function Main()
{
if (stage) { this.init(); }
else { this.addEventListener(Event.ADDED_TO_STAGE, init); }
}
private function init():void
{
stage.addEventListener(MouseEvent.CLICK, stageClicked);
tf.autoSize = TextFieldAutoSize.LEFT;
tf.text = "click the stage to begin the test";
addChild(tf);
}
private var tf:TextField = new TextField();
private var instanceConst:Number = 42;
private var instanceVar:Number = 42;
private static const staticConst:Number = 42;
private static var staticVar:Number = 42;
private function row(...c): void { tf.appendText(c.join(",")+"\n"); }
private function stageClicked(ev:MouseEvent):void
{
tf.text = "";
var REPS:int = 100000000;
var i:int;
var temp:Number;
const localConst:Number = 42;
var localVar:Number = 42;
var instance:MyClass = new MyClass();
row("Variable", "Time");
// ---- OTHER ----
startTest("other static const");
for (i = 0; i < REPS; ++i)
{
temp = MyClass.staticConst;
}
endTest();
startTest("other static var");
for (i = 0; i < REPS; ++i)
{
temp = MyClass.staticVar;
}
endTest();
startTest("other instance const");
for (i = 0; i < REPS; ++i)
{
temp = instance.instanceConst;
}
endTest();
startTest("other instance var");
for (i = 0; i < REPS; ++i)
{
temp = instance.instanceVar;
}
endTest();
// ---- THIS ----
startTest("this static const");
for (i = 0; i < REPS; ++i)
{
temp = staticConst;
}
endTest();
startTest("this static var");
for (i = 0; i < REPS; ++i)
{
temp = staticVar;
}
endTest();
startTest("this instance const");
for (i = 0; i < REPS; ++i)
{
temp = instanceConst;
}
endTest();
startTest("this instance var");
for (i = 0; i < REPS; ++i)
{
temp = instanceVar;
}
endTest();
// ---- LOCAL ----
startTest("local const");
for (i = 0; i < REPS; ++i)
{
temp = localConst;
}
endTest();
startTest("local var");
for (i = 0; i < REPS; ++i)
{
temp = localVar;
}
endTest();
}
private var currentTest:String = "";
private var currentTestTime:uint;
private function startTest(name:String):void
{
currentTest = name;
currentTestTime = getTimer();
}
private function endTest():void
{
row(currentTest, (getTimer() - currentTestTime));
}
}
}
package
{
public class MyClass
{
public static const staticConst:Number = 42;
public static var staticVar:Number = 42;
public const instanceConst:Number = 42;
public var instanceVar:Number = 42;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment