<?php
if (PHP_VERSION !== '7.0.0RC4') { exit('done'); }
class Bench {
protected $functions = [];
public function add($label, \Closure $function) {
$this->functions[] = [$label, $function];
}
public function run($count) {
$function = function () {};
$stats = $this->getStats();
for ($i = $count; $i--;) $function();
$stats2 = $this->getStats();
$timeDiff = $stats2[4] - $stats[4];
$this->printStats('Empty run (time subtracted from real runs)', $stats, $stats2, 0);
// TODO: Split runs in pieces and interleave, to avoid bias with first/last runner.
foreach ($this->functions as list($label, $function)) {
$stats = $this->getStats();
for ($i = $count; $i--;) $function();
$stats2 = $this->getStats();
$this->printStats($label, $stats, $stats2, $timeDiff);
}
}
protected function getStats() {
return [memory_get_usage(), memory_get_usage(true), memory_get_peak_usage(), memory_get_peak_usage(true), microtime(1)];
}
protected function printStats($label, $stats1, $stats2, $timeDiff) {
list($mem, $memReal, $memPeak, $memRealPeak, $time) = $stats1;
list($memNew, $memRealNew, $memPeakNew, $memRealPeakNew, $timeNew) = $stats2;
$nl = "\n";
$labelLength = 20;
$resultsLength = 15;
echo '<p><b>' . $label . ':</b></p>' . $nl;
echo '<code><p>' . $nl;
echo str_pad('Memory', $labelLength, '.', STR_PAD_RIGHT);
echo str_pad(number_format(($memNew - $mem) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl;
echo str_pad('Memory Real', $labelLength, '.', STR_PAD_RIGHT);
echo str_pad(number_format(($memRealNew - $memReal) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl;
echo str_pad('Memory Peak', $labelLength, '.', STR_PAD_RIGHT);
echo str_pad(number_format(($memPeakNew - $memPeak) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl;
echo str_pad('Memory Real Peak', $labelLength, '.', STR_PAD_RIGHT);
echo str_pad(number_format(($memRealPeakNew - $memRealPeak) >> 10, 0), $resultsLength, '.', STR_PAD_LEFT) . ' kb<br>' . $nl;
echo str_pad('Time', $labelLength, '.', STR_PAD_RIGHT);
echo str_pad(number_format($timeNew - $time - $timeDiff, 5), $resultsLength, '.', STR_PAD_LEFT);
echo ' sec' . $nl . '</code></p>' . $nl . $nl;
}
}
$f1 = function ($input) {
return $input * 10;
};
$f2 = function ($input) {
return [true, $input * 10];
};
$b = new Bench();
$b->add('single', function () use ($f1) {
$value = $f1(10);
});
$b->add('tuple', function () use ($f2) {
list($error, $value) = $f2(10);
});
$b->run(40000);
- Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0
- done
- Output for 5.4.0 - 5.4.45
- Parse error: syntax error, unexpected 'list' (T_LIST) in /in/pbrjh on line 23
Process exited with code 255.
preferences:
147.89 ms | 401 KiB | 133 Q