- var_dump: documentation ( source)
- microtime: documentation ( source)
- memory_get_usage: documentation ( source)
- number_format: documentation ( source)
<?php
class Example
{
public function __construct()
{
}
public static function create()
{
return new self();
}
public function doStuff()
{
}
public static function doStaticStuff()
{
}
}
$named = function () {
Example::create()->doStuff();
};
$static = function () {
Example::doStaticStuff();
};
$test = function ($count) use ($named, $static) {
$loop = function ($count, $callable) {
$time_start = microtime(true) * 1000;
$memory_start = memory_get_usage(true);
for ($i = 0; $i < $count; $i++) {
$callable();
}
$time_end = microtime(true) * 1000;
$memory_end = memory_get_usage(true);
return [
$time_end - $time_start,
$memory_end - $memory_start,
];
};
$static_delta = $loop($count, $static);
$named_delta = $loop($count, $named);
echo 'After ' . number_format($count) . " calls:\n";
echo 'Difference in milliseconds: ';
var_dump($named_delta[0] - $static_delta[0]);
echo 'Difference in memory usage: ';
var_dump($named_delta[1] - $static_delta[1]);
echo "-----------------------------\n";
};
$test(1);
$test(10);
$test(100);
$test(1000);
$test(10000);
$test(100000);