AS3のコンパイラは定数畳み込みをやってくれない
/**
* Copyright k0rin ( http://wonderfl.net/user/k0rin )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/p82o
*/
package
{
import flash.display.Sprite;
import flash.text.*;
import flash.utils.*;
public class Main extends Sprite
{
private var output:TextField = new TextField();
public function Main():void
{
benchmark(test1, "for (var i:int = 0; i < 1000 * 10000; i++) {}");
benchmark(test2, "for (var i:int = 0; i < 10000000; i++) {}");
benchmark(test3, "n += 100 * 100;");
benchmark(test4, "n += 10000;");
var format:TextFormat = new TextFormat();
format.font = "_等幅";
format.size = 12;
output.setTextFormat(format);
output.autoSize = TextFieldAutoSize.LEFT;
addChild(output);
}
private function println(string:String):void
{
output.appendText(string + "\n");
}
private function benchmark(func:Function, name:String = ""):void
{
var startTime:int = getTimer();
func();
println((" " + (getTimer() - startTime)).substr(-8, 8) + " ms : " + name);
}
private function test1():void
{
for (var i:int = 0; i < 1000 * 10000; i++) {
}
}
private function test2():void
{
for (var i:int = 0; i < 10000000; i++) {
}
}
private function test3():void
{
var n:int = 0;
for (var i:int = 0; i < 10000000; i++) {
n += 100 * 100;
}
}
private function test4():void
{
var n:int = 0;
for (var i:int = 0; i < 10000000; i++) {
n += 10000;
}
}
}
}