所以我正在尝试使用debug_backtrace
和PHP反射在我的架构中实现面向方面的设计。设计有效,但我决定看看它对性能的影响有多严重,所以我写了下面的分析测试。有趣的是,当Advisable
和NonAdvisable
方法什么也不做时,影响大约是使用可取方法的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
有什么想法吗?
答案 0 :(得分:0)
当您调用A的Advisable方法时,您正在跳过所有迭代...您只需调用一次继承的advise()方法就会覆盖它。因此,当您添加迭代时,您只是将它们添加到NonAdvisable()调用中。
答案 1 :(得分:0)
method overhead应该适用于PHP以及Java我猜 - &#34;调用的实际方法是在运行时确定的#34; =&GT;阴影Advisible
方法的开销更大
但它将是O(1)而不是Non-Advisable
&#39; s O(n)
答案 2 :(得分:0)
很抱歉,在调用父方法之前,我在return null;
方法中找到了行get_advice
。我讨厌回答我自己的问题,但不值得别人搜索它。