In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

[最適化 Tips] 変数を参照する際の処理速度の違い

/**
 * Copyright muta244 ( http://wonderfl.net/user/muta244 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/38XN
 */

package {

import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
import flash.utils.*;

use namespace ns;

public class Main extends Sprite
{
    static private const _NUM_TIMES:uint = 1000000;
    
    static private const PI:Number = Math.PI;
    
    private function get _getPi():Number
    {
        return _pi;
    }
    
    private var _pi:Number = Math.PI;
    ns var _pi:Number = Math.PI;
    
    private function _init():void
    {
        _debug(
            "各テスト " + _NUM_TIMES + " 回処理させた計算結果 [単位 : ミリ秒]\n" +
            "(誤差は多少生じます)\n"
        );
        
        var pi:Number = Math.PI;
        
        _measure("ループのみ", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                
            }
        });
        
        _measure("ビルトインクラスの静的変数 (Math.PI)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                Math.PI;
            }
        });
        
        _measure("オリジナルクラスの静的変数 (MyMath.PI)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                MyMath.PI;
            }
        });
        
        _measure("自クラスの静的変数 (Main.PI)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                Main.PI;
            }
        });
        
        _measure("自クラスの静的変数 (PI)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                PI;
            }
        });
        
        _measure("自クラスの getter メンバ (_getPi)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                _getPi;
            }
        });
        
        _measure("自クラスの private メンバ (_pi)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                _pi;
            }
        });
        
        _measure("自クラスの独自ネームスペースでのメンバ (ns::_pi)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                ns::_pi;
            }
        });
        
        _measure("ローカル変数 (pi)", function ():void
        {
            for (var i:int = 0; i < _NUM_TIMES; i++) {
                pi;
            }
        });
        
        _debug("\n結果については言及しませんので, 各自ご判断ください.");
    }
    
    private var _field:TextField;
    private var _time:uint;
    
    public function Main():void
    {
        _setup();
        _init();
    }
    
    private function _measure(title:String, func:Function, ...params):void
    {
        _time = getTimer();
        func.apply(null, params);
        _time = getTimer() - _time;
        
        _debug("[ " + title + " ] --> " + _time + " ms");
    }
    
    private function _debug(log:String):void
    {
        _field.appendText(log + "\n");
    }
    
    private function _setup():void
    {
        _field = new TextField();
        _field.width = stage.stageWidth - 40;
        _field.height = stage.stageHeight - 60;
        _field.x = 20;
        _field.y = 60;
        _field.multiline = true;
        _field.wordWrap = true;
        
        var format:TextFormat = _field.defaultTextFormat;
        format.font = "_sans";
        _field.defaultTextFormat = format;
        
        addChild(_field);
        
        var button:Sprite = new Sprite();
        button.graphics.lineStyle(1, 0xBBBBBB);
        button.graphics.beginFill(0xEEEEEE);
        button.graphics.drawRoundRect(0, 0, 100, 20, 5, 5);
        button.graphics.endFill();
        
        addChild(button);
        
        button.x = 20;
        button.y = 20;
        button.mouseChildren = false;
        button.buttonMode = true;
        
        var field:TextField = new TextField();
        field.width = 100;
        field.height = 20;
        field.htmlText = "<p align='center'><font face='_sans'>再計算</span></p>";
        
        button.addChild(field);
        
        button.addEventListener(MouseEvent.CLICK, function ():void
        {
            _field.text = "";
            _init();
        });
    }
}

}

namespace ns = "original namespace";

class MyMath
{
    static public const PI:Number = Math.PI;
}