<?php
class A {
public static function test() {
return true;
}
}
function test() {
static $a;
return $a ??= new A;
}
$its = 1e3;
$test = test();
$t = hrtime(true);
for ( $i=0;$i++<$its;)
test()->test();
echo (hrtime(true)-$t)/1e9,"s\n";
$t = hrtime(true);
for ( $i=0;$i++<$its;)
$test->test();
echo (hrtime(true)-$t)/1e9,"s\n";
$t = hrtime(true);
for ( $i=0;$i++<$its;)
A::test();
echo (hrtime(true)-$t)/1e9,"s\n";
echo "\n=== Warmed up ===\n\n";
$its = 1e6;
$t = hrtime(true);
for ( $i=0;$i++<$its;)
test()->test();
echo $a=(hrtime(true)-$t)/1e9,"s\n";
$t = hrtime(true);
for ( $i=0;$i++<$its;)
$test->test();
echo $b=(hrtime(true)-$t)/1e9,"s\n";
$t = hrtime(true);
for ( $i=0;$i++<$its;)
A::test();
echo $c=(hrtime(true)-$t)/1e9,"s\n";
echo number_format((($a-$c)/$a) * 100, 2 ), "% faster from function\n";
echo number_format((($b-$c)/$b) * 100, 2 ), "% faster from variable\n";
preferences:
27.31 ms | 405 KiB | 5 Q