0

AS3 の式を文字列に変換する方法はありますか?

Flash AS3 ファイルのボトルネックを見つけるための非常に小規模なプロファイラーを作成しています。現在の構文は次のとおりです (これは面倒です)。

PerformanceProfiler.start("updatePlayer");
updatePlayer(map, enemies);
PerformanceProfiler.stop();

私は最終的に、後で呼び出しPerformanceProfiler.show()から結果を取得するために呼び出しtraceます。

内部的に、は、渡された合計ミリ秒のリストにPerformanceProfiler、 などの関数の名前でリンクされた辞書を保持します。関数は、関数名とそれぞれに費やされた合計時間をリストするだけです"updatePlayer"show

私のニーズでは、これでうまくいきます。

"updatePlayer"ただし、繰り返しになるため、すべてを避けたいと思います。start実際、私はandstop呼び出しを完全に取り除きたいと思っています。

私の考えは、ある種の匿名関数で呼び出すことでした (申し訳ありませんが、その機能の正確な構文についてはわかりません):

PerformanceProfiler.profile( { updatePlayer(map, enemies); } );

そして、この方法でのみ結果を表示します:

{ updatePlayer(マップ、敵); } - 1202

{ updateEnemies(マップ、プレイヤー); } - 5126

...

では、匿名メソッドの内容を文字列として取得する方法はありますか? これは素晴らしいことです。ただし、私の問題に対するより簡単な解決策があれば、私は本当に感謝します.フレームレートの低下が発生しているため、現在は見つけることができず、単純に1%かかる可能性のあるものを最適化するために時間を無駄にしたくないからです.処理時間の。

要約すると、私が欲しいのは次のとおりです。

{ updatePlayer(map, enemies); }"{ updatePlayer(map, enemies); }"AS3 に変換されます。

貴重な時間をありがとうございました。

4

3 に答える 3

3

それを行う簡単な方法があり、プロファイラーなしでどの関数が呼び出されたかを知ることはできないと思います。

ただし、 Namespace.Class.Function 名を読み取り、「エラー」と「引数」を使用してパラメーターをチェックする関数を作成できます。

public function someFunction (param1:Object , param2:Object ):void {
   Profile(arguments);
   // function body ...
}

////////

public function Profile (args:Object):void {
    trace(new Error().getStackTrace()); // from this string You will read func name
    for each(var o:* in args) {
    trace("param:",o); // here are parameters
    }
}
于 2012-10-04T06:49:20.200 に答える
1

あなたの質問に正確に答えるわけではありませんが、flash.sampler パッケージをチェックしてください: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/sampler/package-detail.html

これを使用すると、 を呼び出す必要はまったくなくPerformanceProfiler、返されたサンプルを解析するだけで済みます。

それについて簡単に知りたい場合は、マイナーをチェックしてください: http://www.sociodox.com/theminer/index.html

プロジェクトに追加するのは非常に簡単で、このすべてのサンプル/プロファイリングが既にあります

于 2012-10-04T12:48:21.213 に答える
0

なぜ地獄ではないので、別の答え:)

package  
{
    import flash.display.Sprite;
    import flash.system.System;
    import flash.utils.describeType;
    import flash.utils.Dictionary;
    import flash.utils.getTimer;

    public class TestDescribeType extends Sprite
    {
        private var m_cache:Dictionary      = null; // a cache that says if we've used this object before
        private var m_functions:Dictionary  = null; // the functions for this object

        public function TestDescribeType() 
        {
            this.m_cache        = new Dictionary;
            this.m_functions    = new Dictionary;

            this.profile( this, this.one );
            this.profile( this, this.two, 4.5 );
            this.profile( this, this.three, 4.5, "hello" );
            this.profile( this, this.four, 4.5, "hello" );
        }

        // profiles how long it takes a function to complete
        public function profile( obj:*, func:Function, ...params ):void
        {
            // if we don't have this object in our cache, parse it
            if ( !( obj in this.m_cache ) )
                this._parse( obj );

            // get our functions dictionary and get our function name
            var dict:Dictionary = this.m_functions[obj];
            var funcName:String = ( dict != null && ( func in dict ) ) ? dict[func] : "Unknown func";

            // start our timer and call our function
            var startTime:int = getTimer();
            func.apply( null, params );
            trace( "  " + funcName + " took " + ( getTimer() - startTime ) + "ms to finish" );
        }

        // some test functions
        public function one():void { trace( "one()" ) }
        public function two( one:Number ):void { trace( "two(" + one + ")" ) }
        public function three( one:Number, two:String ):void { trace( "three(" + one + ", " + two + ")" ) }
        public function four( one:Number, two:String, three:int = 0 ):void { trace( "four(" + one + ", " + two + ", " + three + ")" ) }

        // parses a class/object and removes all the functions from it
        private function _parse( obj:* ):void
        {
            // get our xml
            var x:XML           = describeType( obj );
            this.m_cache[obj]   = true; // we don't need to store the xml, just that we've parsed it

            // find our functions
            var funcs:Dictionary = new Dictionary;
            for each( var mx:XML in x.method )
            {
                var name:String     = mx.@name;
                var func:Function   = obj[name] as Function;

                // store the function with the name etc in our dictionary
                funcs[func] = name;
            }

            // store our functions
            this.m_functions[obj] = funcs;

            // kill our xml object immediately - helps memory
            System.disposeXML( x );
        }

    }

}

コードを実行して、その機能を理解してください。基本的にdescribeType()、オブジェクトに最初に遭遇したときにオブジェクトに対して a を実行し、そのすべての機能を取り出します。次に、profile関数は必要なものを取得します。キャッシュをチェックアウトして正しい名前を取得し、それを呼び出します。

オブジェクトにエラー (イニシャルdescribeType()) をスローするよりも高速であるという利点がありますが、これは 1 回限りであり、本当に必要な場合は前処理ステップを追加できます。

がオブジェクトではなくクラスでのみ機能するように、それを作り直す必要がありdescribeType()ます (10 個のスプライトがあるかのように、10describeType()秒かかります)。そうしないとm_functions(func in dict)最初のインスタンスを除くオブジェクトのすべてのインスタンスでチェックが失敗します。

とにかく、あなたはアイデアを得る:D

于 2012-10-05T09:46:14.810 に答える