optimization
最適化Tips 配列の要素にアクセスする時にASで型変換 forked from: 最適化Tips 配列の要素にアクセスする時にASで型変換
- <?xml version="1.0" encoding="utf-8" ?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" applicationComplete="appCompleteHandler(event)">
- <mx:Script>
- <![CDATA[
- import flash.events.MouseEvent;
- import mx.events.FlexEvent;
- import mx.controls.Alert;
- import flash.system.Capabilities;
- import flash.utils.getTimer;
- import flash.geom.Point;
- private var loopCount:int = 1000000;
- private var ary:Array;
- public function appCompleteHandler(event:FlexEvent):void{
- if(Capabilities.isDebugger){
- var msg:String = "デバッグ版のプレーヤを使用しています。正しいベンチマークの結果を得るにはリリース版を使用することをお勧めします。";
- Alert.show(msg, "インフォメーション");
- }
- console.text += "OS : " + Capabilities.os + "\n";
- console.text += "Vesion : " + Capabilities.version + "\n";
- console.text += "Type : " + Capabilities.playerType + "\n";
- Capabilities.isDebugger ? console.text += "Debug version\n" : null;
- }
- public function clickHandler(event:MouseEvent):void{
- startBenchmark();
- }
- public function clear(event:MouseEvent):void {
- console.text = "";
- }
- public function startBenchmark():void{
- var time:int;
- var dt:int;
- var i:int;
- var p:Point;
- ary = new Array();
- ary.push(new Point());
- i = 0;
- time = getTimer();
- while(i < loopCount){
- (p = Point(ary[0])).x = 0;
- p.y = 0;
- i++;
- }
- dt = getTimer() - time;
- console.text += "キャストする場合" + dt + "ms\n";
- i = 0;
- time = getTimer();
- while(i < loopCount){
- (p = ary[0] as Point).x = 0;
- p.y = 0;
- i++;
- }
- dt = getTimer() - time;
- console.text += "ASによるキャスト" + dt + "ms\n";
- i = 0;
- time = getTimer();
- while(i < loopCount){
- (p = ary[0]).x = 0;//微妙にasの方が早い?
- p.y = 0;
- i++;
- }
- dt = getTimer() - time;
- console.text += "キャストしない場合" + dt + "ms\n";
- i = 0;
- time = getTimer();
- while(i < loopCount){
- p = ary[0];
- p.x = 0;
- p.y = 0;
- i++;
- }
- dt = getTimer() - time;
- console.text += "あまり.で繋げない場合" + dt + "ms\n";
- i = 0;
- time = getTimer();
- while(i < loopCount){
- with(ary[0]){
- x = 0;
- y = 0;
- }
- i++;
- }
- dt = getTimer() - time;
- console.text += "あかん" + dt + "ms\n";
- console.text += "\n";
- }
- ]]>
- </mx:Script>
- <mx:VBox width="100%" height="100%">
- <mx:TextArea id="console" width="100%" height="100%"></mx:TextArea>
- <mx:HBox width="100%">
- <mx:Button label="Start" click="clickHandler(event)" />
- <mx:Button label="Clear" click="clear(event)" />
- </mx:HBox>
- </mx:VBox>
- </mx:Application>
notice: 
