3v4l.org

run code in 300+ PHP versions simultaneously
<?php class IteratorWithFilter { public $generator; public function __construct(array $data) { $this->generator = new \ArrayIterator($data); } public function filter() { $weakRef = WeakReference::create($this); $this->generator = new CallbackFilterIterator($this->generator, static function(array $row) use ($weakRef) { $filterObj = $weakRef->get(); return $filterObj->matchRow($row); }); $this->generator->filterObj = $this; // this assigment will link the objects exactly the same as if // bound function is passed directly, thus the WeakReference // will always be valid/nonGCed return $this; } public function matchRow(array $row) { return $row[0] === 'b'; } } function createIterator() { $iter = new IteratorWithFilter([['a'], ['b']]); return $iter->filter(); } for ($i = 0; $i < 10 * 1000; $i++) { $generator = createIterator()->generator; iterator_to_array($generator); // must success and always return ['b'] if (($i % 10) === 0) { gc_collect_cycles(); } if (($i % 1000) === 0) { echo round(memory_get_usage() / (1024 * 1024), 3) . " MiB\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)
7.4.100.0280.08116.55
7.4.90.0300.09516.41
7.4.80.0270.09016.55
7.4.70.0220.07316.57
7.4.60.0220.07516.40
7.4.50.0120.07216.66
7.4.40.0100.08316.30
7.4.30.0180.08716.63
7.4.20.0350.08616.47
7.4.10.0100.06916.62
7.4.00.0230.07916.22
7.3.220.0080.01916.43
7.3.210.0140.01216.41
7.3.200.0090.01616.38
7.3.190.0150.01216.23
7.3.180.0110.01516.38
7.3.170.0160.00816.35
7.3.160.0170.01416.41
7.3.150.0240.00016.41
7.3.140.0120.01316.36
7.3.130.0090.01316.41
7.3.120.0120.01216.56
7.3.110.0100.01416.52
7.3.100.0170.00616.30
7.3.90.0050.01916.21
7.3.80.0120.01016.49
7.3.70.0150.00616.33
7.3.60.0120.00916.42
7.3.50.0130.01216.45
7.3.40.0120.01216.35
7.3.30.0160.00516.36
7.3.20.0100.02116.48
7.3.10.0180.00416.40
7.3.00.0120.01116.37
7.2.330.0190.00316.46
7.2.320.0160.00916.46
7.2.310.0060.01416.40
7.2.300.0130.01216.56
7.2.290.0130.01216.42
7.2.280.0030.02116.65
7.2.270.0090.00516.52
7.2.260.0100.02116.42
7.2.250.0080.01816.71
7.2.240.0130.01216.55
7.2.230.0030.01816.55
7.2.220.0120.01216.44
7.2.210.0070.01716.80
7.2.200.0120.01316.68
7.2.190.0200.00616.45
7.2.180.0080.00616.31
7.2.170.0150.01216.43
7.2.160.0110.01516.46
7.2.150.0230.00416.48
7.2.140.0120.01516.59
7.2.130.0180.00716.54
7.2.120.0110.01516.61
7.2.110.0140.01216.53
7.2.100.0130.01416.43
7.2.90.0250.01816.49
7.2.80.0170.00716.39
7.2.70.0170.00916.37
7.2.60.0260.01416.45
7.2.50.0120.01316.52
7.2.40.0130.00816.64
7.2.30.0200.00516.63
7.2.20.0180.00816.50
7.2.10.0150.01316.69
7.2.00.0080.01816.66

preferences:
41.05 ms | 400 KiB | 5 Q