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 (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 $context->getControllerPrefix() . $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->requestPath = isset($config['requestPath']) ? $config['requestPath'] : array(); $this->appMode = isset($config['appMode']) ? $config['appMode'] : 'DEVELOPMENT'; $this->logger = isset($config['logger']) ? $config['logger'] : null; $this->controllerPrefix = isset($config['controllerPrefix']) ? $config['controllerPrefix'] : null; } public function getRequestURI() { return $this->requestURI; } public function getRequestPath() { if (!is_array($this->requestPath)) { $this->requestPath = explode('/', str_replace('\\', '/', $this->requestURI)); } return $this->requestPath; } public function getAppMode() { return $this->appMode; } public function getLogger() { return $this->logger; } public function getControllerPrefix() { return $this->controllerPrefix; } } class RootController { public $home = 'homeController'; public function __invoke($args = array()) { return "Root controller index"; } } $context = new Context([ 'requestURI' => '/' ]); $dispatch = new ObjectDispatch(); foreach ($dispatch($context, 'rootController') as $signal) { echo var_export($signal, true) . "\n"; }

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.0140.00716.88
8.3.50.0130.00821.22
8.3.40.0090.00618.95
8.3.30.0120.00319.02
8.3.20.0040.00420.29
8.3.10.0000.00823.66
8.3.00.0040.00421.83
8.2.180.0090.00616.75
8.2.170.0120.00322.96
8.2.160.0170.00320.55
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0050.00321.12
8.2.120.0060.00326.35
8.2.110.0030.00719.44
8.2.100.0100.00318.18
8.2.90.0030.00519.34
8.2.80.0040.00417.97
8.2.70.0100.00017.84
8.2.60.0050.00518.18
8.2.50.0050.00518.09
8.2.40.0040.00422.32
8.2.30.0030.00519.63
8.2.20.0050.00317.89
8.2.10.0050.00318.24
8.2.00.0030.00518.12
8.1.280.0130.01325.92
8.1.270.0030.00622.31
8.1.260.0080.00028.09
8.1.250.0080.00028.09
8.1.240.0070.00322.29
8.1.230.0090.00318.02
8.1.220.0080.00017.78
8.1.210.0060.00318.77
8.1.200.0070.00317.48
8.1.190.0000.00817.47
8.1.180.0000.00818.10
8.1.170.0080.00018.71
8.1.160.0040.00420.80
8.1.150.0040.00420.84
8.1.140.0050.00319.77
8.1.130.0000.00717.74
8.1.120.0000.01117.50
8.1.110.0080.00017.51
8.1.100.0040.00417.49
8.1.90.0090.00017.48
8.1.80.0070.00017.49
8.1.70.0070.00017.57
8.1.60.0040.00417.72
8.1.50.0090.00017.68
8.1.40.0050.00317.61
8.1.30.0050.00317.62
8.1.20.0080.00017.65
8.1.10.0050.00317.73
8.1.00.0050.00317.61
8.0.300.0040.00418.77
8.0.290.0000.00817.00
8.0.280.0030.00318.63
8.0.270.0030.00417.39
8.0.260.0000.00817.38
8.0.250.0030.00317.15
8.0.240.0000.00816.99
8.0.230.0030.00517.15
8.0.220.0080.00016.98
8.0.210.0080.00017.04
8.0.200.0030.00317.18
8.0.190.0090.00017.16
8.0.180.0000.00817.13
8.0.170.0080.00017.17
8.0.160.0000.00717.09
8.0.150.0040.00417.07
8.0.140.0040.00416.94
8.0.130.0030.00313.55
8.0.120.0000.00816.98
8.0.110.0040.00416.91
8.0.100.0040.00417.02
8.0.90.0080.00017.13
8.0.80.0130.00317.14
8.0.70.0040.00417.11
8.0.60.0040.00417.15
8.0.50.0050.00216.98
8.0.30.0070.01017.30
8.0.20.0120.01117.45
8.0.10.0070.00017.25
8.0.00.0130.00416.95
7.4.330.0000.00516.90
7.4.320.0000.00616.67
7.4.300.0000.00716.55
7.4.290.0000.00916.71
7.4.280.0060.00316.63
7.4.270.0000.00716.59
7.4.260.0070.00316.64
7.4.250.0040.00416.59
7.4.240.0070.00016.71
7.4.230.0000.00716.83
7.4.220.0090.00916.75
7.4.210.0060.01216.69
7.4.200.0040.00416.62
7.4.160.0120.00316.69
7.4.150.0080.01617.40
7.4.140.0100.01217.86
7.4.130.0120.00816.67
7.4.120.0070.01016.65
7.4.110.0060.01216.76
7.4.100.0080.01316.65
7.4.90.0150.00316.71
7.4.80.0110.01119.39
7.4.70.0090.00616.76
7.4.60.0080.00816.81
7.4.50.0070.01016.60
7.4.40.0180.00016.45
7.4.30.0110.00816.63
7.4.10.0060.01314.96
7.4.00.0080.00815.15
7.3.330.0030.00313.42
7.3.320.0000.00613.34
7.3.310.0000.00716.66
7.3.300.0000.00816.56
7.3.290.0050.00216.45
7.3.280.0080.00916.60
7.3.270.0130.01017.40
7.3.260.0110.00816.54
7.3.250.0110.01116.52
7.3.240.0090.01016.52
7.3.230.0130.00516.59
7.3.210.0200.00316.52
7.3.200.0090.00919.39
7.3.190.0130.00316.54
7.3.180.0170.00716.55
7.3.170.0140.00416.65
7.3.160.0100.00716.61
7.3.130.0070.01115.08
7.3.120.0080.01115.01
7.3.110.0030.01214.88
7.3.100.0050.00914.95
7.3.90.0050.00715.12
7.3.80.0070.00514.94
7.3.70.0080.00614.67
7.3.60.0020.01015.00
7.3.50.0070.00714.84
7.3.40.0050.00714.79
7.3.30.0020.01214.93
7.3.20.0040.00816.75
7.3.10.0060.00716.56
7.3.00.0050.00516.72
7.2.330.0100.00717.01
7.2.320.0110.00716.84
7.2.310.0140.00316.75
7.2.300.0090.00916.65
7.2.290.0080.00816.64
7.2.260.0100.01015.10
7.2.250.0020.01614.97
7.2.240.0080.00815.42
7.2.230.0080.01015.22
7.2.220.0050.00615.06
7.2.210.0090.00315.17
7.2.200.0050.00815.16
7.2.190.0060.00815.03
7.2.180.0080.00815.20
7.2.170.0060.00815.24
7.2.160.0030.01415.27
7.2.150.0080.00416.77
7.2.140.0030.01016.77
7.2.130.0000.01116.91
7.2.120.0080.00716.89
7.2.110.0050.00816.90
7.2.100.0050.00716.88
7.2.90.0020.01116.90
7.2.80.0070.00816.74
7.2.70.0060.00616.93
7.2.60.0080.00716.97
7.2.50.0030.01016.90
7.2.40.0020.01016.93
7.2.30.0040.00916.86
7.2.20.0060.00717.01
7.2.10.0080.00716.97
7.2.00.0050.00717.77
7.1.330.0080.00515.71
7.1.320.0080.00415.76
7.1.310.0060.00715.92
7.1.300.0090.00615.79
7.1.290.0020.00715.68
7.1.280.0050.00715.78
7.1.270.0070.00915.72
7.1.260.0030.00615.75
7.1.250.0060.00815.78
7.1.240.0080.00715.75
7.1.230.0030.00815.88
7.1.220.0070.00715.75
7.1.210.0010.01015.66
7.1.200.0040.00715.84
7.1.190.0040.00715.72
7.1.180.0090.00515.67
7.1.170.0030.01015.80
7.1.160.0020.01015.72
7.1.150.0080.00615.79
7.1.140.0100.00315.77
7.1.130.0040.01015.67
7.1.120.0070.00715.66
7.1.110.0040.00715.83
7.1.100.0040.01016.43
7.1.90.0060.00615.74
7.1.80.0000.01315.81
7.1.70.0030.01116.29
7.1.60.0070.01116.84
7.1.50.0070.00716.22
7.1.40.0080.00815.77
7.1.30.0000.01015.74
7.1.20.0100.00515.79
7.1.10.0040.01015.72
7.1.00.0080.02518.06
7.0.330.0060.00615.33
7.0.320.0040.00715.34
7.0.310.0080.00315.44
7.0.300.0080.00515.16
7.0.290.0040.00615.36
7.0.280.0040.01015.41
7.0.270.0040.00715.24
7.0.260.0020.00915.33
7.0.250.0110.00315.29
7.0.240.0020.00915.24
7.0.230.0070.00415.41
7.0.220.0070.00515.29
7.0.210.0040.00615.49
7.0.200.0110.00515.36
7.0.190.0070.00615.38
7.0.180.0030.00615.20
7.0.170.0030.00915.29
7.0.160.0090.00615.43
7.0.150.0040.01115.34
7.0.140.0050.00915.24
7.0.130.0070.00715.49
7.0.120.0070.00515.41
7.0.110.0030.00715.39
7.0.100.0060.01916.75
7.0.90.0110.01816.78
7.0.80.0070.03216.84
7.0.70.0050.03016.89
7.0.60.0090.01517.01
7.0.50.0100.02317.24
7.0.40.0060.01915.58
7.0.30.0050.01915.69
7.0.20.0040.02415.74
7.0.10.0060.03015.62
7.0.00.0040.03115.60
5.6.400.0070.00714.80
5.6.390.0100.00714.70
5.6.380.0050.00914.42
5.6.370.0020.01314.39
5.6.360.0060.00814.61
5.6.350.0080.00614.42
5.6.340.0080.00714.38
5.6.330.0080.00814.50
5.6.320.0080.00614.50
5.6.310.0110.00314.67
5.6.300.0000.01214.43
5.6.290.0030.01214.44
5.6.280.0030.02116.74
5.6.270.0030.01014.60
5.6.260.0070.00714.40
5.6.250.0070.03116.63
5.6.240.0080.03116.57
5.6.230.0080.01716.58
5.6.220.0040.02416.55
5.6.210.0060.03016.49
5.6.200.0050.03516.74
5.6.190.0080.03016.61
5.6.180.0120.02016.76
5.6.170.0060.03216.91
5.6.160.0040.01916.73
5.6.150.0070.02816.81
5.6.140.0070.03016.73
5.6.130.0080.02616.63
5.6.120.0000.02116.59
5.6.110.0070.02916.63
5.6.100.0030.03016.50
5.6.90.0090.01416.68
5.6.80.0090.02916.54
5.6.70.0050.01816.38
5.6.60.0030.02516.42
5.6.50.0080.02916.45
5.6.40.0090.02516.48
5.6.30.0030.03416.37
5.6.20.0050.02716.38
5.6.10.0110.03016.45
5.6.00.0100.02616.40
5.5.380.0090.02815.34
5.5.370.0090.03115.43
5.5.360.0090.02915.51
5.5.350.0080.02815.32
5.5.340.0040.02915.57
5.5.330.0020.02915.48
5.5.320.0020.02115.60
5.5.310.0040.03215.69
5.5.300.0080.02915.60
5.5.290.0050.03315.57
5.5.280.0040.02215.63
5.5.270.0030.03515.48
5.5.260.0080.02715.52
5.5.250.0060.02915.39
5.5.240.0040.02315.31
5.5.230.0040.01815.35
5.5.220.0070.02015.17
5.5.210.0060.03015.38
5.5.200.0070.02715.43
5.5.190.0050.03215.32
5.5.180.0080.02815.29
5.5.170.0070.00312.82
5.5.160.0080.02915.24
5.5.150.0070.02015.27
5.5.140.0090.02815.20
5.5.130.0070.02915.31
5.5.120.0050.02415.32
5.5.110.0050.03115.40
5.5.100.0090.02615.24
5.5.90.0120.02015.34
5.5.80.0070.01815.19
5.5.70.0040.02315.23
5.5.60.0020.02715.25
5.5.50.0070.02715.42
5.5.40.0050.02015.33
5.5.30.0070.03215.30
5.5.20.0020.02815.36
5.5.10.0050.02315.19
5.5.00.0060.02615.11

preferences:
52.43 ms | 401 KiB | 5 Q