2

debug_backtraceそのため、PHP リフレクションを使用して、アスペクト指向設計をアーキテクチャに実装しようとしています。設計は機能しますが、それがパフォーマンスにどれほど悪影響を与えるかを確認することにしたので、次のプロファイリング テストを作成しました。興味深いことに、 メソッドAdvisableNonAdvisableメソッドが何もしない場合、推奨される方法を使用した場合と推奨されない方法を使用した場合の約 5 倍の影響がありますが、各メソッドの複雑さを増すと (ここでは反復回数を増やして30 以上)、推奨される方法のパフォーマンスが向上し始め、複雑さが増すにつれてパフォーマンスが向上し続けます。

基本クラス:

abstract class Advisable {

    private static $reflections = array();
    protected static $executions = 25;

    protected static function advise()
    {
        $backtrace = debug_backtrace();
        $method_trace = $backtrace[1];
        $object = $method_trace['object'];
        $function = $method_trace['function'];
        $args = $method_trace['args'];

        $class = get_called_class();

        // We'll introduce this later
        $before = array();
        $around = array();
        $after = array();

        $method_info = array(
            'args' => $args,
            'object' => $object,
            'class' => $class,
            'method' => $function,
            'around_queue' => $around
        );

        array_unshift($args, $method_info);
        foreach ($before as $advice)
        {
            call_user_func_array($advice, $args);
        }

        $result = self::get_advice($method_info);

        foreach ($after as $advice)
        {
            call_user_func_array($advice, $args);
        }

        return $result;
    }

    public static function get_advice($calling_info)
    {
        if ($calling_info['around_queue'])
        {
            $around = array_shift($calling_info['around_queue']);
            if ($around)
            {
                // a method exists in the queue
                return call_user_func_array($around, array_merge(array($calling_info), $calling_info['args']));
            }
        }
        $object = $calling_info['object'];
        $method = $calling_info['method'];
        $class = $calling_info['class'];

        if ($object)
        {
            return null; // THIS IS THE OFFENDING LINE
            // this is a class method
            if (isset(self::$reflections[$class][$method]))
            {
                $parent = self::$reflections[$class][$method];
            }
            else
            {
                $parent = new ReflectionMethod('_'.$class, $method);
                if (!isset(self::$reflections[$class]))
                {
                    self::$reflections[$class] = array();
                }
                self::$reflections[$class][$method] = $parent;
            }
            return $parent->invokeArgs($object, $calling_info['args']);
        }
        // this is a static method
        return call_user_func_array(get_parent_class($class).'::'.$method, $calling_info['args']);
    }
}

実装されたクラス:

abstract class _A extends Advisable
{
    public function Advisable()
    {
        $doing_stuff = '';
        for ($i = 0; $i < self::$executions; $i++)
        {
            $doing_stuff .= '.';
        }
        return $doing_stuff;
    }

    public function NonAdvisable()
    {
        $doing_stuff = '';
        for ($i = 0; $i < self::$executions; $i++)
        {
            $doing_stuff .= '.';
        }
        return $doing_stuff;
    }
}

class A extends _A
{
    public function Advisable()
    {
        return self::advise();
    }
}

そして、メソッドをプロファイリングします:

$a = new A();
$start_time = microtime(true);
$executions = 1000000;
for ($i = 0; $i < $executions; $i++)
{
    $a->Advisable();
}
$advisable_execution_time = microtime(true) - $start_time;
$start_time = microtime(true);
for ($i = 0; $i < $executions; $i++)
{
    $a->NonAdvisable();
}
$non_advisable_execution_time = microtime(true) - $start_time;

echo 'Ratio: '.$advisable_execution_time/$non_advisable_execution_time.'<br />';
echo 'Advisable: '.$advisable_execution_time.'<br />';
echo 'Non-Advisable: '.$non_advisable_execution_time.'<br />';
echo 'Overhead: '.($advisable_execution_time - $non_advisable_execution_time);

複雑度 100 (A::executions = 100) でこのテストを実行すると、次の結果が得られます。

Ratio: 0.289029437803
Advisable: 7.08797502518
Non-Advisable: 24.5233671665
Overhead: -17.4353921413

何か案は?

4

3 に答える 3

0

メソッドのオーバーヘッドは、JavaだけでなくPHPにも適用されるはずです-「呼び出される実際のメソッドは実行時に決定されます」=>シャドウされたAdvisibleメソッドのオーバーヘッドはより大きくなります

Non-Advisableただし、 's O(n)ではなくO(1)になります

于 2012-03-16T21:08:34.937 に答える
0

A の Advisable メソッドを呼び出すと、すべての反復がスキップされます...継承された advisor() メソッドへの 1 回の呼び出しで上書きされます。したがって、反復を追加するときは、それらを NonAdvisable() 呼び出しに追加するだけです。

于 2012-03-16T21:03:39.657 に答える
0

親メソッドが呼び出される前にメソッドreturn null;内の行を見つけました。get_advice私は自分の質問に答えるのが嫌いですが、他の誰かがそれを検索する価値はありません.

于 2012-03-16T21:23:05.587 に答える