3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Database { public function query($query) { return "Database#query($query) result."; } } class SimpleKeyValCache implements Cache { private $caches = array(); public function get($key) { if (!array_key_exists($key, $this->caches)) { throw new Exception("Key Does not exist"); } return $this->caches[$key]; } public function set($key, $value) { $this->caches[$key] = $value; } } class UselessKeyValCache implements Cache { public function get($key) { throw new Exception("Key Does not exist"); } public function set($key, $value) { // NOOP; } } interface Cache { public function get($key); public function set($key, $value); } interface CacheManager { public function get($key); } class SimpleCacheManager implements CacheManager { private $cache; private $db; public function __construct(Cache $cache, Database $db) { $this->cache = $cache; $this->db = $db; } public function get($key) { try { return $this->cache->get($key); } catch (Exception $e) { $value = $this->db->query("$key"); $this->cache->set($key, $value); return $value; } } } class Api { private $manager; public function __construct(CacheManager $manager) { $this->manager = $manager; } public function getResponse() { $result["A"] = $this->manager->get("One"); $result["B"] = $this->manager->get("Two"); $result["C"] = $this->manager->get("One"); return $result; } } class DoStuff { private $api; public function __construct(Api $api, array $scalarValue) { $this->api = $api; } public function doThatStuff() { var_dump($this->api->getResponse()); } } class DoSomeOtherStuff { private $api; public function __construct(Api $api) { $this->api = $api; } public function doThatOtherStuff() { var_dump($this->api->getResponse() + $this->api->getResponse()); } } class ConstructorInjectionContainer { private $types; private $config; public function __construct($config) { $this->config = $config; } /** * Construct a type using configuration information. * * The worst case time complexity of this algorithm is O(n^n) as we are making n recursive calls in n loops. Although in practice this _should_ never be a huge issue as n will never be larger than the sum of all dependency's constructor's arguments. * */ public function resolve($type, $overrides = null) { // TODO: error handling for invalid types. // Get the concrete type name. $concreteType = $this->config[$type]["concrete"]; // Reflect the concrete type so we can dynamically create arguments and subsequently instantiate it. $reflectedType = new ReflectionClass($concreteType); $constructor = $reflectedType->getConstructor(); $paramArray = array(); // If the class has a constructor implementation then generate and instantiate the dependencies dynamically. if ($constructor !== null) { $constructorParams = $constructor->getParameters(); foreach($constructorParams as $param) { $paramArray[$param->name] = $this->getParamValue($type, $param, $overrides); } } return $reflectedType->newInstanceArgs($paramArray); } private function getParamValue($type, $param, $overrides) { // Check overrides first, then check config. if ($overrides !== null && isset($overrides[$type]) && array_key_exists($param->name, $overrides[$type])) { return $overrides[$type][$param->name]; } elseif (array_key_exists($param->name, $this->config[$type]["constructor"])) { return $this->resolve($this->config[$type]["constructor"][$param->name], $overrides); } elseif (array_key_exists($this->config[$type]["concrete"], $this->config)) { return $this->resolve($this->config[$type]["concrete"], $overrides); } else { throw new Exception("Value for $type constructor parameter {$param->name} was not defined in overrides or configuration."); } } } $config = <<<'JSON' { "Cache": { "concrete": "SimpleKeyValCache", "constructor": { } }, "CacheManager": { "concrete": "SimpleCacheManager", "constructor": { "cache": "Cache" } }, "Api": { "concrete": "Api", "constructor": { "manager": "CacheManager" } }, "DoStuff": { "concrete": "DoStuff", "constructor": { "api": "Api" } }, "DoSomeOtherStuff": { "concrete": "DoSomeOtherStuff", "constructor": { "api": "CacheManager" } } } JSON; $di = new ConstructorInjectionContainer(json_decode($config, true)); $database = new Database(); $dostuff = $di->resolve("DoStuff", array( "CacheManager" => array( "db" => $database ), "DoStuff" => array( "scalarValue" => array() ) ) )->doThatStuff(); exit;

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.0060.01617.00
8.3.50.0110.00818.19
8.3.40.0080.00818.98
8.3.30.0090.00618.89
8.3.20.0090.00018.73
8.3.10.0060.00320.52
8.3.00.0130.00317.75
8.2.180.0070.01316.63
8.2.170.0000.01522.96
8.2.160.0070.00722.26
8.2.150.0060.00324.18
8.2.140.0070.00024.66
8.2.130.0070.00026.16
8.2.120.0080.00019.95
8.2.110.0060.00322.08
8.2.100.0040.00817.97
8.2.90.0080.00018.16
8.2.80.0090.00017.97
8.2.70.0080.00017.63
8.2.60.0040.00418.03
8.2.50.0110.00018.07
8.2.40.0080.00018.22
8.2.30.0050.00318.19
8.2.20.0040.00417.96
8.2.10.0040.00417.83
8.2.00.0000.00817.80
8.1.280.0070.01425.92
8.1.270.0150.00024.66
8.1.260.0000.00726.35
8.1.250.0080.00028.09
8.1.240.0050.00522.26
8.1.230.0060.00622.67
8.1.220.0040.00417.89
8.1.210.0030.00518.99
8.1.200.0090.00017.35
8.1.190.0000.00817.64
8.1.180.0000.00818.10
8.1.170.0000.00918.83
8.1.160.0040.00419.06
8.1.150.0030.00618.64
8.1.140.0040.00417.52
8.1.130.0030.00317.80
8.1.120.0030.00517.42
8.1.110.0030.00517.40
8.1.100.0080.00017.35
8.1.90.0000.00717.54
8.1.80.0000.00717.49
8.1.70.0040.00417.41
8.1.60.0080.00417.65
8.1.50.0040.00417.50
8.1.40.0060.00317.40
8.1.30.0020.00517.72
8.1.20.0000.00817.63
8.1.10.0050.00317.61
8.1.00.0040.00417.64
8.0.300.0060.00320.07
8.0.290.0050.00316.88
8.0.280.0000.00718.39
8.0.270.0040.00417.33
8.0.260.0030.00316.83
8.0.250.0040.00417.13
8.0.240.0000.00917.04
8.0.230.0050.00316.97
8.0.220.0000.00817.05
8.0.210.0050.00316.96
8.0.200.0070.00017.05
8.0.190.0030.00517.15
8.0.180.0040.00417.07
8.0.170.0040.00417.08
8.0.160.0030.00617.11
8.0.150.0000.00817.03
8.0.140.0000.00917.04
8.0.130.0030.00313.49
8.0.120.0040.00416.96
8.0.110.0040.00416.91
8.0.100.0040.00417.04
8.0.90.0040.00416.93
8.0.80.0150.00016.91
8.0.70.0040.00417.05
8.0.60.0000.00716.91
8.0.50.0000.00816.93
8.0.30.0060.01316.99
8.0.20.0090.01617.25
8.0.10.0050.00317.18
8.0.00.0110.01316.95
7.4.330.0000.00513.12
7.4.320.0040.00416.72
7.4.300.0030.00316.66
7.4.290.0040.00416.59
7.4.280.0040.00416.65
7.4.270.0000.00716.59
7.4.260.0050.00013.33
7.4.250.0040.00416.48
7.4.240.0040.00416.58
7.4.230.0030.00316.37
7.4.220.0070.02016.69
7.4.210.0080.00916.62
7.4.200.0080.00016.73
7.4.190.0050.00316.61
7.4.160.0110.00716.66
7.4.150.0150.01216.57
7.4.140.0090.00916.60
7.4.130.0100.00716.66
7.4.120.0110.00716.64
7.4.110.0100.00716.50
7.4.100.0150.00316.72
7.4.90.0090.00916.37
7.4.80.0100.00716.55
7.4.70.0120.01216.57
7.4.60.0130.00916.52
7.4.50.0000.00516.66
7.4.40.0090.00616.48
7.4.30.0110.01116.63
7.4.10.0040.00815.00
7.4.00.0070.01015.17
7.3.330.0030.00313.43
7.3.320.0030.00313.39
7.3.310.0080.00016.54
7.3.300.0030.00316.57
7.3.290.0110.00416.61
7.3.280.0090.00716.54
7.3.270.0110.00916.50
7.3.260.0050.01616.76
7.3.250.0070.01016.61
7.3.240.0140.00316.46
7.3.230.0090.00916.51
7.3.210.0030.01316.59
7.3.200.0160.00619.39
7.3.190.0100.00716.72
7.3.180.0130.00316.55
7.3.170.0090.00916.65
7.3.160.0060.01016.53
7.3.130.0060.00914.90
7.3.120.0080.00814.91
7.3.110.0070.01114.89
7.3.100.0080.00415.14
7.3.90.0000.01815.05
7.3.80.0040.00714.82
7.3.70.0000.01614.89
7.3.60.0040.01615.03
7.3.50.0070.00714.71
7.3.40.0090.00014.84
7.3.30.0000.01614.92
7.3.20.0090.00616.81
7.3.10.0030.00916.65
7.3.00.0100.00716.60
7.2.330.0120.00616.76
7.2.320.0030.01416.55
7.2.310.0100.01016.95
7.2.300.0070.01116.82
7.2.290.0060.01016.72
7.2.260.0060.01315.33
7.2.250.0100.00715.09
7.2.240.0030.01015.26
7.2.230.0040.00815.07
7.2.220.0030.00815.07
7.2.210.0000.01414.87
7.2.200.0110.00415.10
7.2.190.0070.01314.96
7.2.180.0070.00715.14
7.2.170.0000.01515.17
7.2.160.0060.00915.17
7.2.150.0070.00716.74
7.2.140.0100.00716.95
7.2.130.0030.00716.71
7.2.120.0100.00316.98
7.2.110.0030.00917.01
7.2.100.0100.00616.95
7.2.90.0030.00616.96
7.2.80.0000.01417.05
7.2.70.0060.01016.91
7.2.60.0050.00816.82
7.2.50.0070.00716.90
7.2.40.0060.00916.94
7.2.30.0090.00617.01
7.2.20.0060.00816.91
7.2.10.0000.01417.05
7.2.00.0030.01517.04
7.1.330.0000.01315.93
7.1.320.0140.00315.78
7.1.310.0030.01015.59
7.1.300.0110.00315.83
7.1.290.0040.00815.70
7.1.280.0090.00615.96
7.1.270.0000.00915.85
7.1.260.0000.01415.60
7.1.250.0040.00815.91
7.1.240.0040.00715.93
7.1.230.0060.00615.91
7.1.220.0040.00715.51
7.1.210.0080.00415.99
7.1.200.0100.00315.85
7.1.190.0070.01115.79
7.1.180.0000.00915.68
7.1.170.0060.00615.64
7.1.160.0030.00615.61
7.1.150.0140.00015.88
7.1.140.0070.01015.54
7.1.130.0080.00315.83
7.1.120.0080.00415.94
7.1.110.0130.00316.03
7.1.100.0060.00615.67
7.1.90.0090.00315.67
7.1.80.0060.00615.78
7.1.70.0050.00716.43
7.1.60.0080.01117.49
7.1.50.0080.01125.16
7.1.40.0000.00915.93
7.1.30.0070.00715.72
7.1.20.0050.00815.57
7.1.10.0000.00915.75
7.1.00.0050.04119.13
7.0.330.0060.00615.18
7.0.320.0030.01315.29
7.0.310.0030.00915.10
7.0.300.0030.00615.45
7.0.290.0000.00915.20
7.0.280.0120.00315.57
7.0.270.0090.00015.20
7.0.260.0090.00615.13
7.0.250.0030.01315.54
7.0.240.0000.00815.51
7.0.230.0000.00815.52
7.0.220.0040.00915.52
7.0.210.0060.00615.65
7.0.200.0030.00914.93
7.0.190.0110.00815.65
7.0.180.0080.00415.34
7.0.170.0000.01115.57
7.0.160.0090.00615.55
7.0.150.0040.00815.57
7.0.140.0090.03318.74
7.0.130.0080.00815.50
7.0.120.0030.04018.76
7.0.110.0030.00915.23
7.0.100.0130.04017.65
7.0.90.0070.04017.75
7.0.80.0030.04717.73
7.0.70.0050.04417.81
7.0.60.0020.04617.59
7.0.50.0150.04717.96
7.0.40.0070.03016.74
7.0.30.0050.04016.79
7.0.20.0030.04316.78
7.0.10.0070.02916.79
7.0.00.0000.03116.76
5.6.400.0060.00914.42
5.6.390.0070.00714.59
5.6.380.0060.00614.82
5.6.370.0140.00014.54
5.6.360.0030.01414.54
5.6.350.0060.00614.41
5.6.340.0070.00714.59
5.6.330.0040.01114.57
5.6.320.0080.00514.84
5.6.310.0080.00814.17
5.6.300.0060.00614.67
5.6.290.0060.00914.47
5.6.280.0020.04417.72
5.6.270.0090.00314.65
5.6.260.0070.00714.68
5.6.250.0070.03017.69
5.6.240.0070.03817.67
5.6.230.0050.04017.62
5.6.220.0020.03117.52
5.6.210.0030.05017.54
5.6.200.0050.05017.85
5.6.190.0050.04617.82
5.6.180.0080.03817.81
5.6.170.0150.04317.87
5.6.160.0060.04817.69
5.6.150.0050.03017.79
5.6.140.0060.04117.82
5.6.130.0050.04717.88
5.6.120.0100.04217.79
5.6.110.0070.04717.96
5.6.100.0050.04417.83
5.6.90.0050.06117.94
5.6.80.0100.04317.50
5.6.70.0080.04017.49
5.6.60.0040.04117.54
5.6.50.0120.03717.41
5.6.40.0040.02917.53
5.6.30.0070.04217.50
5.6.20.0110.04017.41
5.6.10.0070.02217.42
5.6.00.0120.01817.40
5.5.380.0140.03717.52
5.5.370.0030.02517.31
5.5.360.0070.03017.43
5.5.350.0080.02817.68
5.5.340.0090.02517.86
5.5.330.0070.02517.91
5.5.320.0110.03517.64
5.5.310.0100.02517.66
5.5.300.0100.04217.72
5.5.290.0070.02917.47
5.5.280.0070.05417.74
5.5.270.0040.05017.55
5.5.260.0020.05217.72
5.5.250.0160.02617.56
5.5.240.0040.04417.33
5.5.230.0070.04217.34
5.5.220.0080.04117.41
5.5.210.0110.02317.47
5.5.200.0040.02217.37
5.5.190.0040.02217.41
5.5.180.0070.02317.48
5.5.170.0100.00614.23
5.5.160.0080.04017.21
5.5.150.0070.02517.28
5.5.140.0050.02517.49
5.5.130.0050.04417.15
5.5.120.0110.01717.47
5.5.110.0060.04017.38
5.5.100.0050.02517.34
5.5.90.0090.02417.38
5.5.80.0070.02417.25
5.5.70.0100.03717.19
5.5.60.0110.03617.19
5.5.50.0080.04317.39
5.5.40.0060.02617.30
5.5.30.0030.03117.19
5.5.20.0060.02717.14
5.5.10.0050.03117.31
5.5.00.0030.04117.33
5.4.450.0060.04115.42
5.4.440.0020.02815.33
5.4.430.0060.03815.32
5.4.420.0080.03415.45
5.4.410.0070.04115.36
5.4.400.0070.03215.12
5.4.390.0090.03915.09
5.4.380.0100.03815.14
5.4.370.0080.03515.19
5.4.360.0030.03515.27
5.4.350.0050.02015.09
5.4.340.0070.04215.21
5.4.330.0090.00611.49
5.4.320.0070.01815.11
5.4.310.0020.04615.16
5.4.300.0150.03015.03
5.4.290.0100.03515.27
5.4.280.0100.03515.01
5.4.270.0080.02914.97
5.4.260.0090.02215.01
5.4.250.0030.04315.25
5.4.240.0030.02815.08
5.4.230.0080.03115.08
5.4.220.0120.02515.10
5.4.210.0060.03015.19
5.4.200.0080.03515.20
5.4.190.0070.04215.16
5.4.180.0120.03915.10
5.4.170.0080.02315.23
5.4.160.0080.03615.02
5.4.150.0070.02715.14
5.4.140.0050.03413.87
5.4.130.0020.04513.68
5.4.120.0030.02014.05
5.4.110.0050.02213.82
5.4.100.0020.02913.95
5.4.90.0070.03813.86
5.4.80.0100.03413.90
5.4.70.0090.03713.78
5.4.60.0100.03514.00
5.4.50.0030.03213.89
5.4.40.0030.02013.81
5.4.30.0020.02213.88
5.4.20.0050.02313.72
5.4.10.0070.01813.75
5.4.00.0020.03713.60
5.3.290.0020.03712.73
5.3.280.0050.04312.77
5.3.270.0050.03412.59
5.3.260.0090.03812.74
5.3.250.0050.03912.66
5.3.240.0050.03712.64
5.3.230.0050.03212.63
5.3.220.0020.02612.69
5.3.210.0050.05012.65
5.3.200.0080.03512.64
5.3.190.0050.02012.71
5.3.180.0070.03212.67
5.3.170.0060.02012.74
5.3.160.0020.04712.70
5.3.150.0020.04312.79
5.3.140.0050.03912.71
5.3.130.0020.02312.69
5.3.120.0010.03312.71
5.3.110.0100.03812.74
5.3.100.0080.04112.45
5.3.90.0030.03512.42
5.3.80.0050.02612.20
5.3.70.0090.02412.31
5.3.60.0070.03312.47
5.3.50.0040.02112.36
5.3.40.0020.04212.34
5.3.30.0030.02812.29
5.3.20.0050.01812.29
5.3.10.0020.04112.17
5.3.00.0100.03512.12

preferences:
44.25 ms | 400 KiB | 5 Q