3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Collection extends ArrayObject { /** * @var string */ protected $expectedValueType = ''; /** * Collection constructor. * * @param string $expectedValueType * @param array $initialValues */ public function __construct(string $expectedValueType, array $initialValues = []) { $this->setExpectedValueType($expectedValueType); foreach ($initialValues as $initialValue) $this->append($initialValue); } /** * @param $value */ public function append($value) { if (!$this->validateType($value)) throw new \InvalidArgumentException('Expected value type for this collection is ' . $this->getExpectedValueType() . ', ' . gettype($value) . ' given.'); parent::append($value); } /** * @param mixed $value * * @return bool */ public function contains($value): bool { foreach ($this as $v) { if ($v == $value) return true; } return false; } /** * @param $value * * @return false|int|string|mixed */ public function getKey($value) { foreach ($this as $key => $v) { if ($v == $value) return $key; } return false; } /** * @inheritdoc */ public function offsetSet($offset, $value) { if (!$this->validateType($value)) throw new \InvalidArgumentException('Expected value type for this collection is ' . $this->getExpectedValueType() . ', ' . gettype($value) . ' given.'); parent::offsetSet($offset, $value); } /** * @param mixed $value */ public function remove($value) { if (!$this->contains($value)) throw new \InvalidArgumentException('The given value does not exist in this collection.'); $this->offsetUnset($this->getKey($value)); } /** * @param $value * * @return bool */ public function validateType($value): bool { $expectedValueType = $this->getExpectedValueType(); switch ($expectedValueType) { case 'string': return is_string($value); case 'int': case 'integer': return is_int($value); case 'float': case 'double': return is_float($value); case 'array': return is_array($value); case 'object': return is_object($value); case 'callable': return is_callable($value); default: return ($value instanceof $expectedValueType); } } /** * @return string */ public function getExpectedValueType(): string { return $this->expectedValueType; } /** * @param string $expectedValueType */ public function setExpectedValueType(string $expectedValueType) { if (in_array(strtolower($expectedValueType), ['string', 'int', 'integer', 'float', 'double', 'bool', 'boolean', 'array', 'object', 'callable'])) $expectedValueType = strtolower($expectedValueType); $this->expectedValueType = $expectedValueType; } } $coll = new Collection ('string',['a','b']); foreach($coll as $item){ var_dump($item); }

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.0090.01218.56
8.3.50.0070.00716.38
8.3.40.0100.01018.80
8.3.30.0140.00018.84
8.3.20.0000.00821.90
8.3.10.0040.00421.86
8.3.00.0040.00418.25
8.2.180.0070.01418.41
8.2.170.0150.00322.96
8.2.160.0140.00021.05
8.2.150.0050.00324.18
8.2.140.0060.00324.66
8.2.130.0040.00422.11
8.2.120.0080.00026.35
8.2.110.0060.00321.08
8.2.100.0110.00417.91
8.2.90.0040.00417.63
8.2.80.0040.00417.97
8.2.70.0000.00917.50
8.2.60.0050.00317.50
8.2.50.0000.00818.04
8.2.40.0060.00318.16
8.2.30.0040.00417.79
8.2.20.0040.00418.14
8.2.10.0040.00418.02
8.2.00.0020.00518.14
8.1.280.0150.00025.92
8.1.270.0030.00523.79
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0120.00017.48
8.1.230.0050.00617.89
8.1.220.0050.00317.74
8.1.210.0050.00518.77
8.1.200.0030.00717.48
8.1.190.0050.00317.36
8.1.180.0030.00518.10
8.1.170.0060.00317.76
8.1.160.0080.00018.97
8.1.150.0000.00720.33
8.1.140.0030.00519.02
8.1.130.0040.00417.57
8.1.120.0070.00017.59
8.1.110.0000.00717.45
8.1.100.0040.00417.53
8.1.90.0050.00317.55
8.1.80.0040.00417.53
8.1.70.0000.00717.58
8.1.60.0000.00817.64
8.1.50.0040.00417.51
8.1.40.0030.00517.50
8.1.30.0040.00417.73
8.1.20.0000.00817.72
8.1.10.0080.00017.68
8.1.00.0000.00917.59
8.0.300.0070.00018.77
8.0.290.0000.00916.75
8.0.280.0000.00718.13
8.0.270.0000.00918.10
8.0.260.0000.00716.81
8.0.250.0030.00316.92
8.0.240.0070.00017.00
8.0.230.0000.00817.07
8.0.220.0000.00816.97
8.0.210.0000.00816.92
8.0.200.0070.00017.04
8.0.190.0060.00316.94
8.0.180.0040.00416.97
8.0.170.0000.00817.00
8.0.160.0040.00416.93
8.0.150.0050.00216.93
8.0.140.0080.00016.85
8.0.130.0000.00713.31
8.0.120.0100.00016.92
8.0.110.0040.00416.93
8.0.100.0040.00416.80
8.0.90.0040.00416.82
8.0.80.0190.02216.91
8.0.70.0040.00416.82
8.0.60.0040.00416.91
8.0.50.0000.00716.86
8.0.30.0080.01217.04
8.0.20.0130.00917.40
8.0.10.0080.00016.98
8.0.00.0110.00716.93
7.4.330.0000.00515.55
7.4.320.0000.00616.70
7.4.300.0000.00716.59
7.4.290.0050.00316.57
7.4.280.0040.00416.69
7.4.270.0030.00316.56
7.4.260.0110.00016.62
7.4.250.0040.00416.65
7.4.240.0080.00016.72
7.4.230.0030.00316.74
7.4.220.0170.00016.77
7.4.210.0040.01116.72
7.4.200.0000.00716.77
7.4.160.0090.00616.59
7.4.150.0090.00917.40
7.4.140.0110.00817.86
7.4.130.0050.01516.46
7.4.120.0110.00916.70
7.4.110.0040.01416.73
7.4.100.0060.01216.59
7.4.90.0090.00916.75
7.4.80.0110.01119.39
7.4.70.0110.00516.77
7.4.60.0030.01316.54
7.4.50.0100.00616.50
7.4.40.0150.00416.51
7.4.30.0110.00716.49
7.4.00.0000.01515.25
7.3.330.0030.00513.31
7.3.320.0030.00313.35
7.3.310.0000.00716.54
7.3.300.0000.00716.38
7.3.290.0060.00816.34
7.3.280.0090.00816.37
7.3.270.0030.01517.40
7.3.260.0100.00816.45
7.3.250.0100.00816.50
7.3.240.0070.00916.36
7.3.230.0070.01016.45
7.3.210.0110.01116.43
7.3.200.0030.01416.64
7.3.190.0100.00916.46
7.3.180.0140.01016.62
7.3.170.0070.00816.80
7.3.160.0140.00716.27
7.3.10.0080.00616.52
7.3.00.0130.00316.66
7.2.330.0030.01416.88
7.2.320.0030.01516.69
7.2.310.0060.01116.65
7.2.300.0110.00816.87
7.2.290.0160.00016.48
7.2.130.0100.00317.01
7.2.120.0130.00316.77
7.2.110.0060.00616.89
7.2.100.0100.00717.00
7.2.90.0130.00316.45
7.2.80.0100.00716.82
7.2.70.0140.00716.77
7.2.60.0080.00516.80
7.2.50.0150.00516.70
7.2.40.0130.01016.78
7.2.30.0060.01216.33
7.2.20.0130.00716.37
7.2.10.0100.01116.41
7.2.00.0080.00817.06
7.1.250.0050.00515.42
7.1.200.0040.01115.57
7.1.70.0000.01717.12
7.1.60.0600.01033.41
7.1.50.0930.01333.06
7.1.40.0900.01332.60
7.1.30.0570.01732.38
7.1.20.0500.02032.90
7.1.10.0470.01014.71
7.1.00.0830.01314.78
7.0.200.0570.01015.04
7.0.190.0770.01314.95
7.0.180.0430.00714.41
7.0.170.0530.01014.34
7.0.160.0500.01314.54
7.0.150.0430.01314.45
7.0.140.0530.01314.46
7.0.130.0630.00714.71
7.0.120.0570.01014.84
7.0.110.0500.01014.62
7.0.100.0470.01014.50
7.0.90.0500.01014.47
7.0.80.0770.01714.54
7.0.70.0600.00714.48
7.0.60.0800.01314.44
7.0.50.0470.01314.66
7.0.40.0800.01714.79
7.0.30.0600.00714.62
7.0.20.0630.01014.72
7.0.10.0570.00314.88
7.0.00.0800.01314.80

preferences:
50.95 ms | 400 KiB | 5 Q