Is there a compelling reason to not use debug_backtrace for the sole purpose of determining the calling method\'s class, name, and parameter list? Not for debugging purposes
I did another performance test of debug_backtrace
method. To simulate real stacks i defined several classes which call another class methods randomly - with a specified DEPTH:
a = $foo;
$bar->a = $foo;
$foo->a = $bar;
$alice->b = $bar;
$foo->b = $alice;
$bar->b = $alice;
$this->a = $foo;
$this->b = $bar;
}
public function method($depth)
{
BACKTRACE ? debug_backtrace() : null;
$obj = mt_rand(0,1) === 1 ? $this->a : $this->b;
if ($depth > DEPTH) {
return;
}
$obj->method($depth+1);
}
}
class Foo
{
public $a;
public $b;
public function method($depth)
{
BACKTRACE ? debug_backtrace() : null;
$obj = mt_rand(0,1) === 1 ? $this->a : $this->b;
if ($depth > DEPTH) {
return;
}
$obj->method($depth+1);
}
}
class Bar
{
public $a;
public $b;
public function method($depth)
{
BACKTRACE ? debug_backtrace() : null;
$obj = mt_rand(0,1) === 1 ? $this->a : $this->b;
if ($depth > DEPTH) {
return;
}
$obj->method($depth+1);
}
}
class Alice
{
public $a;
public $b;
public function method($depth)
{
BACKTRACE ? debug_backtrace() : null;
$obj = mt_rand(0,1) === 1 ? $this->a : $this->b;
if ($depth > DEPTH) {
return;
}
$obj->method($depth+1);
}
}
$test = new TestClass();
$startWhole = microtime(true);
for($i = 0; $i < 10000;$i++) {
$start = microtime(true);
$test->method(0);
$end = microtime(true);
}
$endWhole = microtime(true);
$total = $endWhole - $startWhole;
echo 'total time: ' . $total . "s\n";
Result:
Performance (10.000 iterations / stack depth 30 / PHP 7.2):
with debug_backtrace - total time: 0.86011600494385s
without: - total time: 0.043321847915649s
using debug_backtrace
makes this code 20x times slower.