3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Magnus; class ObjectDispatch { public function routeIterator(&$path) { while (!empty($path)) { yield end($path); array_pop($path); /* This prevents having to put back a value in the event of a * readjustment in the dispatch path. * Testing indicates that it's better to do array maninpulation than it is * to implement SplDoublyLinkedList for deque behavior. Likewise, * simply tracking the index is a bit slower and can add complexity * when dealing with reorients/redispatches. */ } } public function __invoke($context, $root) { $log = $context->getLogger(); $path = $context->getRequestPath(); $last = ''; $parent = null; $current = $root; if ($context->getAppMode() === 'DEBUG' && $log !== null) { $log->addDebug('Starting Object Dispatch', [ 'request' => $context->getRequestURI(), 'path' => var_export($path, true), 'root' => var_export($root, true) ]); } foreach ($this->routeIterator($path) as $chunk) { if ($context->getAppMode() === 'DEBUG' && $log !== null) { $log->addDebug('Beginning dispatch step.', [ 'chunk' => $chunk, 'path' => var_export($path, true), 'current' => var_export($current, true) ]); } if (!is_object($current) || class_exists($context->getControllerPrefix() . $current)) { if ($context->getAppMode() === 'DEBUG' && $log !== null) { $log->addDebug('Instantiating current class', [ 'request' => $context->getRequestURI(), 'current' => $current ]); } $current = $context->getControllerPrefix() . $current($context); } if (is_object($current)) { $parent = $current; } if (in_array($chunk, get_class_methods($parent))) { if ($context->getAppMode() === 'DEBUG' && $log !== null) { $log->addDebug('Found an endpoint', [ 'request' => $context->getRequestURI(), 'isEndpoint' => true, 'parent' => var_export($parent, true), 'handler' => $chunk, 'arguments' => var_export($path, true) ]); } yield array($parent, $chunk, $path, true); } elseif (in_array($chunk, get_object_vars($parent))) { if ($context->getAppMode() === 'DEBUG' && $log !== null) { $log->addDebug('Found a property', [ 'request' => $context->getRequestURI(), 'property' => $chunk, 'parent' => var_export($parent, true) ]); } $current = $parent->chunk; } elseif (method_exists($parent, 'lookup')) { try { list($current, $consumed) = $parent->lookup($path); $chunk = implode('/', $consumed); $path = array_slice($path, 0, count($path) - count($consumed)); } catch (Exception $e) { throw new HTTPNotFound(); } } else { throw new HTTPNotFound(); } yield array(explode('/', $last), $parent, false); $last = $chunk; } if ($context->getAppMode() === 'DEBUG' && $log !== null) { $log->addDebug('No endpoint found', [ 'request' => $context->getRequestURI(), 'current' => var_export($current), 'parent' => var_export($parent) ]); } if (!is_object($current) && class_exists($context->getControllerPrefix() . $current)) { $current = new $current($context); } if (is_callable($current)) { yield array($current, $chunk, $path, true); } elseif (is_callable($parent)) { yield array($parent, $chunk, $path, true); } } } class Context { protected $requestURI; protected $requestPath; protected $appMode; protected $logger; protected $controllerPrefix; public function __construct(Array $config) { $this->requestURI = isset($config['requestURI']) ? $config['requestURI'] : '/'; $this->appMode = isset($config['appMode']) ? $config['appMode'] : 'DEVELOPMENT'; $this->logger = isset($config['logger']) ? $config['logger'] : null; $this->controllerPrefix = isset($config['controllerPrefix']) ? $config['controllerPrefix'] : __NAMESPACE__ . "\\"; echo $this->controllerPrefix; var_dump(class_exists('RootController')); $this->requestPath = explode('/', str_replace('\\', '/', $this->requestURI)); if (end($this->requestPath) === '') { array_pop($this->requestPath); } $this->requestPath = array_reverse($this->requestPath); if (end($this->requestPath) === '') { array_pop($this->requestPath); } } public function getRequestURI() { return $this->requestURI; } public function getRequestPath() { return $this->requestPath; } public function getAppMode() { return $this->appMode; } public function getLogger() { return $this->logger; } public function getControllerPrefix() { return $this->controllerPrefix; } } class ScreenLog { public function __call($methodName, $args) { echo var_export($args, true) . "\n"; } } class RootController { public $home = 'homeController'; public function __invoke($args = array()) { return "Root controller index"; } } $context = new Context([ 'requestURI' => '/', 'logger' => new ScreenLog() ]); $dispatch = new ObjectDispatch(); foreach ($dispatch($context, 'RootController') as $signal) { list($object, $chunk, $path, $isEndpoint) = $signal; if ($isEndpoint) { echo $object->$chunk($path); } }

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)
8.3.60.0120.00317.13
8.3.50.0110.00416.64
8.3.40.0120.00318.79
8.3.30.0090.00619.00
8.3.20.0040.00420.46
8.3.10.0000.00821.88
8.3.00.0080.00022.20
8.2.180.0090.00616.75
8.2.170.0040.01122.96
8.2.160.0100.00320.44
8.2.150.0030.00524.18
8.2.140.0000.00824.66
8.2.130.0080.00020.97
8.2.120.0060.00326.35
8.2.110.0180.00020.69
8.2.100.0060.00618.09
8.2.90.0050.00518.11
8.2.80.0030.00518.34
8.2.70.0040.00417.88
8.2.60.0060.00317.88
8.2.50.0090.00017.63
8.2.40.0000.00817.88
8.2.30.0000.00818.02
8.2.20.0030.00517.95
8.2.10.0040.00419.90
8.2.00.0000.00818.23
8.1.280.0150.00325.92
8.1.270.0080.00022.39
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0030.00622.25
8.1.230.0060.00623.98
8.1.220.0000.00917.79
8.1.210.0050.00319.00
8.1.200.0060.00317.47
8.1.190.0050.00317.48
8.1.180.0000.00818.10
8.1.170.0080.00017.62
8.1.160.0030.00618.99
8.1.150.0040.00419.13
8.1.140.0000.00819.70
8.1.130.0040.00417.75
8.1.120.0000.00817.62
8.1.110.0030.00517.59
8.1.100.0040.00417.59
8.1.90.0050.00317.60
8.1.80.0000.00817.48
8.1.70.0040.00417.55
8.1.60.0000.00817.68
8.1.50.0040.00417.67
8.1.40.0040.00417.70
8.1.30.0030.00617.62
8.1.20.0080.00017.80
8.1.10.0000.00817.63
8.1.00.0030.00517.64
8.0.300.0070.00019.98
8.0.290.0000.00716.75
8.0.280.0040.00418.21
8.0.270.0000.00718.13
8.0.260.0070.00017.29
8.0.250.0030.00517.04
8.0.240.0030.00317.22
8.0.230.0030.00317.11
8.0.220.0040.00416.97
8.0.210.0040.00417.12
8.0.200.0070.00017.07
8.0.190.0000.00817.18
8.0.180.0030.00617.13
8.0.170.0030.00517.02
8.0.160.0070.00017.20
8.0.150.0030.00617.06
8.0.140.0040.00416.93
8.0.130.0040.00413.48
8.0.120.0090.00016.97
8.0.110.0000.00917.00
8.0.100.0000.00816.91
8.0.90.0050.00216.94
8.0.80.0060.00917.16
8.0.70.0080.00017.12
8.0.60.0000.00716.96
8.0.50.0000.00716.95
8.0.30.0120.00617.39
8.0.20.0110.01117.47
8.0.10.0040.00417.33
8.0.00.0070.01116.90
7.4.330.0000.00516.83
7.4.320.0000.00716.59
7.4.300.0040.00416.56
7.4.290.0040.00416.65
7.4.280.0040.00416.63
7.4.270.0040.00416.78
7.4.260.0020.00516.67
7.4.250.0000.00716.71
7.4.240.0040.00416.56
7.4.230.0000.00716.76
7.4.220.0120.00616.66
7.4.210.0090.01216.85
7.4.200.0080.00016.72
7.4.160.0060.01516.68
7.4.150.0160.00317.40
7.4.140.0130.01017.86
7.4.130.0130.01216.64
7.4.120.0100.00916.74
7.4.110.0060.01216.63
7.4.100.0060.01216.81
7.4.90.0070.01016.69
7.4.80.0130.01019.39
7.4.70.0080.00816.50
7.4.60.0060.01016.65
7.4.50.0090.00616.30
7.4.40.0130.00416.70
7.4.30.0100.01416.46
7.4.00.0030.01015.09
7.3.330.0030.00313.53
7.3.320.0030.00313.33
7.3.310.0040.00416.60
7.3.300.0030.00316.46
7.3.290.0100.00716.58
7.3.280.0090.00916.55
7.3.270.0120.00617.40
7.3.260.0140.01116.74
7.3.250.0080.01116.58
7.3.240.0070.01316.56
7.3.230.0070.01016.54
7.3.210.0120.00616.64
7.3.200.0100.00719.39
7.3.190.0070.01416.79
7.3.180.0090.00916.80
7.3.170.0140.00316.52
7.3.160.0060.01216.54
7.2.330.0090.00916.85
7.2.320.0140.01116.83
7.2.310.0220.00416.62
7.2.300.0090.01516.83
7.2.290.0170.00016.74
7.2.110.0190.01216.34
7.2.60.0070.00717.03
7.2.00.0070.01019.59
7.1.200.0030.01015.75
7.1.100.0410.00718.26
7.1.70.0050.00317.02
7.1.60.0130.01319.40
7.1.50.0000.01117.00
7.1.00.0030.07722.50
7.0.200.0100.01014.96
7.0.100.0270.07320.30
7.0.90.0130.06020.02
7.0.80.0030.07020.00
7.0.70.0170.08720.13
7.0.60.0070.05320.10
7.0.50.0070.03720.48
7.0.40.0130.04320.10
7.0.30.0070.08720.08
7.0.20.0130.05019.95
7.0.10.0100.08319.99
7.0.00.0100.05020.05
5.6.280.0030.03720.93
5.6.250.0230.06020.85
5.6.240.0170.07320.73
5.6.230.0100.08320.69
5.6.220.0170.06720.80
5.6.210.0070.08020.52
5.6.200.0030.09321.11
5.6.190.0070.08021.10
5.6.180.0170.07021.03
5.6.170.0100.07021.07
5.6.160.0070.04321.11
5.6.150.0130.08721.10
5.6.140.0070.08321.00
5.6.130.0100.05021.10
5.6.120.0030.08321.11
5.6.110.0000.08721.16
5.6.100.0000.08721.22
5.6.90.0100.06321.11
5.6.80.0100.07720.51
5.6.70.0130.03320.45
5.6.60.0100.07720.45
5.6.50.0030.08020.47
5.6.40.0070.07320.43
5.6.30.0070.08020.45
5.6.20.0100.07720.52
5.6.10.0070.05020.47
5.6.00.0100.08020.51
5.5.380.0070.08320.51
5.5.370.0070.07320.48
5.5.360.0170.07720.41
5.5.350.0100.05020.46
5.5.340.0070.08020.99
5.5.330.0000.09020.98
5.5.320.0130.08020.95
5.5.310.0100.07320.94
5.5.300.0170.06720.89
5.5.290.0000.09020.96
5.5.280.0170.07320.96
5.5.270.0030.08320.86
5.5.260.0130.03320.95
5.5.250.0070.07720.80
5.5.240.0030.04720.32
5.5.230.0030.04020.09
5.5.220.0070.07720.24
5.5.210.0070.08020.32
5.5.200.0130.07320.28
5.5.190.0100.04020.21
5.5.180.0170.07720.18
5.5.160.0070.04720.24
5.5.150.0070.04720.33
5.5.140.0070.07320.14
5.5.130.0030.05020.19
5.5.120.0030.06320.17
5.5.110.0130.07320.05
5.5.100.0070.04720.18
5.5.90.0100.07320.11
5.5.80.0130.06320.11
5.5.70.0130.06320.02
5.5.60.0170.07320.08
5.5.50.0100.05320.23
5.5.40.0100.07320.05
5.5.30.0130.07720.08
5.5.20.0170.06320.12
5.5.10.0100.06020.18
5.5.00.0200.06320.03

preferences:
44.59 ms | 400 KiB | 5 Q