3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Bench { protected $functions = []; protected $cli; public function __construct($cli = false) { $this->cli = $cli; } 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 test (delta time subtracted from subsequent test)', $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; // TODO: Better CLI support. Machine data export. if (PHP_SAPI === 'CLI') { ob_start(); } 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; if (PHP_SAPI === 'CLI') { $content = ob_end_clean(); echo preg_replace('/<.*?>/', '', $content); } } } class Log { private $has = []; // TODO: Can be replaced with a bitmask. private $events = []; public function __construct() { } /* (non-PHPdoc) * @see \Solver\Logging\Log::log() */ public function log(array $event) { $type = $event['type']; if (!isset($this->has[$type])) $this->has[$type] = true; $this->events[] = $event; } /* (non-PHPdoc) * @see \Solver\Logging\MemoryLog::getEvents() */ public function getEvents($types = null) { if ($types === null || !$this->events) { return $this->events; } else { $types = array_flip($types); // Special optimized case: the given $types mask matches or is a superset of the event types we have. $optimized = true; foreach ($this->has as $key => $val) if (!isset($types[$key])) { $optimized = false; break; } if ($optimized) { return $this->events; } else { $events = []; foreach ($this->events as $event) if (isset($types[$event['type']])) $events[] = $event; return $events; } } } /* (non-PHPdoc) * @see \Solver\Logging\MemoryLog::hasEvents() */ public function hasEvents($types = null) { if ($types === null) { return (bool) $this->has; } else { foreach ($types as $type) if (isset($this->has[$type])) return true; return false; } } /* (non-PHPdoc) * @see \Solver\Logging\ErrorMemoryLog::getErrors() */ public function getErrors() { return $this->getEvents(['error']); } /* (non-PHPdoc) * @see \Solver\Logging\ErrorMemoryLog::hasErrors() */ public function hasErrors() { $o = $this->hasEvents(['error']); $this->events = []; $this->has = []; return $o; } /* (non-PHPdoc) * @see \Solver\Logging\ErrorLog::error() */ public function error($path = null, $message = null, $code = null, array $details = null) { $event = ['type' => 'error']; if (isset($path)) $event['path'] = $path; if (isset($message)) $event['message'] = $message; if (isset($code)) $event['code'] = $code; if (isset($details)) $event['details'] = $details; $this->log($event); } } $log = new Log(); $be = new Bench(true); $be2 = new Bench(); $error = new stdClass(); $a = 0; $b = 0; $be->add('Sentinel', function () use ($error, & $a, & $b) { $x = function ($error) { return mt_rand(0, 1) ? 123 : $error; }; $result = $x($error); if ($result === $error) { $a ++; } else { $b ++; } }); $be->add('Tuple', function () use (& $a, & $b) { $x = function () { return mt_rand(0, 1) ? [true, 123] : [false, null]; }; list($error, $result) = $x(); if ($error) { $a ++; } else { $b ++; } }); $be->add('Exception', function () use (& $a, & $b) { $x = function () { if (mt_rand(0, 1)) throw new \Exception(); return 123; }; try { $result = $x(); $a ++; } catch (\Exception $e) { $b ++; } }); $be->add('Log', function () use ($log, & $a, & $b) { $x = function ($log) { if (mt_rand(0, 1)) { $log->error(); return null; } else { return 123; } }; $result = $x($log); if ($log->hasErrors()) { $a ++; } else { $b ++; } }); if (PHP_VERSION === '7.0.0RC4') $be->run(200000); var_dump($a); var_dump($b);

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
7.2.120.0090.00016.64
7.2.110.0110.00316.59
7.2.100.0090.00016.64
7.2.90.0070.00716.64
7.2.80.0030.01016.61
7.2.70.0000.01116.88
7.2.60.0000.01317.01
7.2.50.0060.00917.00
7.2.40.0090.00616.66
7.2.30.0070.00716.95
7.2.20.0000.01516.91
7.2.10.0090.00616.89
7.2.00.0080.00518.12
7.1.240.0100.00315.62
7.1.230.0060.00315.89
7.1.220.0030.00915.78
7.1.210.0030.00615.55
7.1.200.0090.00015.84
7.1.190.0000.01015.45
7.1.180.0000.01415.88
7.1.170.0070.01015.95
7.1.160.0090.00615.62
7.1.150.0030.01315.76
7.1.140.0030.01315.88
7.1.130.0030.00815.79
7.1.120.0040.00415.79
7.1.110.0110.00416.00
7.1.100.0060.00917.07
7.1.90.0060.00615.73
7.1.80.0030.01015.84
7.1.70.0060.00516.68
7.1.60.0050.00716.50
7.1.50.0080.00924.97
7.1.40.0000.01515.82
7.1.30.0070.00715.77
7.1.20.0000.01415.50
7.1.10.0130.00015.48
7.1.00.0060.04019.18
7.0.320.0070.00715.44
7.0.310.0060.00315.46
7.0.300.0000.01215.46
7.0.290.0080.00415.36
7.0.280.0090.00315.45
7.0.270.0060.00615.48
7.0.260.0030.01315.63
7.0.250.0000.00915.46
7.0.240.0090.00015.48
7.0.230.0000.01315.55
7.0.220.0030.01015.43
7.0.210.0060.00915.53
7.0.200.0080.00816.10
7.0.190.0040.00415.52
7.0.180.0040.00815.49
7.0.170.0070.00015.35
7.0.160.0000.01415.43
7.0.150.0120.00015.50
7.0.140.0150.00015.43
7.0.130.0120.00315.27
7.0.120.0070.00715.45
7.0.110.0060.00915.58
7.0.100.0040.04617.69
7.0.90.0180.04517.77
7.0.80.0100.04017.76
7.0.70.0160.03617.80
7.0.60.0080.04517.88
7.0.50.0020.04817.93
7.0.40.0110.04016.88
7.0.30.0120.03716.82
7.0.20.0070.03916.84
7.0.10.0080.03916.76
7.0.00.0020.03416.71
5.6.380.0060.00614.39
5.6.370.0100.00314.87
5.6.360.0000.01414.50
5.6.350.0070.00714.66
5.6.340.0040.01114.91
5.6.330.0070.00314.66
5.6.320.0030.01014.16
5.6.310.0060.00914.89
5.6.300.0000.01514.69
5.6.290.0040.00814.79
5.6.280.0090.00314.30
5.6.270.0070.00714.48
5.6.260.0040.00714.42
5.6.250.0090.04417.65
5.6.240.0050.04617.77
5.6.230.0050.03617.69
5.6.220.0030.03817.66
5.6.210.0070.04217.76
5.6.200.0130.03517.80
5.6.190.0070.04017.85
5.6.180.0030.04617.78
5.6.170.0050.03017.67
5.6.160.0050.04617.78
5.6.150.0050.02717.92
5.6.140.0070.03717.75
5.6.130.0070.04317.93
5.6.120.0030.04717.84
5.6.110.0090.04417.86
5.6.100.0070.04317.81
5.6.90.0090.02717.74
5.6.80.0050.03917.46
5.6.70.0070.03317.40
5.6.60.0020.04517.56
5.6.50.0050.04217.44
5.6.40.0060.03017.32
5.6.30.0050.04617.52
5.6.20.0030.03517.51
5.6.10.0090.03917.38
5.6.00.0050.02817.55
5.5.380.0020.03515.98
5.5.370.0000.04815.96
5.5.360.0080.03615.80
5.5.350.0020.04415.90
5.5.340.0050.04016.19
5.5.330.0080.04216.06
5.5.320.0080.04516.03
5.5.310.0100.03716.21
5.5.300.0080.03815.98
5.5.290.0050.04515.99
5.5.280.0090.04116.01
5.5.270.0040.03216.14
5.5.260.0030.04516.07
5.5.250.0050.04516.08
5.5.240.0060.03315.85
5.5.230.0060.03015.78
5.5.220.0050.04015.86
5.5.210.0070.04115.84
5.5.200.0140.03615.76
5.5.190.0030.04015.85
5.5.180.0050.03715.84
5.5.170.0080.00414.46
5.5.160.0080.04015.85
5.5.150.0060.03815.86
5.5.140.0030.04515.67
5.5.130.0050.04015.69
5.5.120.0050.03415.69
5.5.110.0090.03915.77
5.5.100.0080.04215.71
5.5.90.0080.04015.72
5.5.80.0080.03215.70
5.5.70.0130.03415.72
5.5.60.0080.04015.60
5.5.50.0080.04215.65
5.5.40.0030.03815.63
5.5.30.0060.03815.80
5.5.20.0100.04115.74
5.5.10.0050.04415.68
5.5.00.0060.04015.74
5.4.450.0090.04315.31
5.4.440.0090.03815.31
5.4.430.0030.04615.22
5.4.420.0050.04415.27
5.4.410.0130.03315.11
5.4.400.0080.03815.13
5.4.390.0060.02315.06
5.4.380.0080.03515.13
5.4.370.0070.04015.08
5.4.360.0070.04014.98
5.4.350.0080.03515.01
5.4.340.0000.04515.11
5.4.330.0000.01211.07
5.4.320.0090.04015.14
5.4.310.0050.04015.09
5.4.300.0050.04015.06
5.4.290.0060.03915.06
5.4.280.0050.04215.06
5.4.270.0020.03015.14
5.4.260.0030.04415.12
5.4.250.0080.02215.05
5.4.240.0070.04115.16
5.4.230.0030.03015.12
5.4.220.0030.03514.96
5.4.210.0110.03014.98
5.4.200.0080.02215.06
5.4.190.0070.04015.13
5.4.180.0050.04014.97
5.4.170.0030.04215.09
5.4.160.0030.02215.09
5.4.150.0020.02915.04
5.4.140.0110.03513.79
5.4.130.0030.04013.69
5.4.120.0110.02013.79
5.4.110.0030.03713.70
5.4.100.0020.04313.70
5.4.90.0010.03613.75
5.4.80.0030.04113.81
5.4.70.0050.03513.80
5.4.60.0050.03213.74
5.4.50.0050.03913.72
5.4.40.0080.02013.81
5.4.30.0050.02013.74
5.4.20.0050.03913.69
5.4.10.0110.03513.74
5.4.00.0040.02013.47

preferences:
37.7 ms | 400 KiB | 5 Q