Assigning a class constant to member or local constants
/**
* Copyright Fumio ( http://wonderfl.net/user/Fumio )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jRjz
*/
package {
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.getTimer;
[SWF(width = "240",height = "180")]
public class ConstantTest extends Sprite {
private const COUNT:int = 50000000;
private const PI:Number = Math.PI;
var started:uint;
var my_txt:TextField = new TextField();
var label_txt:TextField = new TextField();
public function ConstantTest() {
// Creating a TextField for display
createTextField();
// Starting test
readClass();
readMember();
readLocal();
}
private function readClass():void {
started = getTimer();
for (var i:int = 0; i < COUNT; i++) {
var temp:Number = Math.PI;
}
xTrace(getTimer() - started);
}
private function readMember():void {
started = getTimer();
for (var i:int = 0; i < COUNT; i++) {
var temp:Number = PI;
}
xTrace(getTimer() - started);
}
private function readLocal():void {
started = getTimer();
const _PI:Number = Math.PI;
for (var i:int = 0; i < COUNT; i++) {
var temp:Number = _PI;
}
xTrace(getTimer() - started);
}
private function createTextField():void {
addChild(my_txt);
addChild(label_txt);
my_txt.autoSize = TextFieldAutoSize.RIGHT;
my_txt.x += 20;
label_txt.autoSize = TextFieldAutoSize.LEFT;
label_txt.text = "Math class:\nmember constant:\nlocal constant:";
}
private function xTrace(n:int):void {
my_txt.appendText(String(n) + "\n");
}
}
}