3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
28.91 ms | 402 KiB | 5 Q