3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = [ 'a', ['b', 'c'], 'd', ['e'], ['f', 'g'], ['h', 'i', ['j', ['k'], ['l', 'm', ['n']]]], 'o', 'p', ['q', 'r'], ['s', ['t', ['u', ['v']]]], 'w', 'x', 'y', 'z']; class TraversableInput implements \IteratorAggregate { public $input = [ 'a', ['b', 'c'], 'd', ['e'], ['f', 'g'], ['h', 'i', ['j', ['k'], ['l', 'm', ['n']]]], 'o', 'p', ['q', 'r'], ['s', ['t', ['u', ['v']]]], 'w', 'x', 'y', 'z']; function getIterator() { return new ArrayIterator($this->input); } } $input = new TraversableInput(); function flatten1($input) { return \array_reduce( (array) $input, function ($carry, $item) { return \is_array($item) ? \array_merge($carry, flatten1($item)) : \array_merge($carry, [$item]); }, [] ); } function flatten2($input) { $stack = [$input]; $result = []; while (!empty($stack)) { $item = array_shift($stack); if (is_array($item) || $item instanceof Traversable) { foreach ($item as $element) { array_unshift($stack, $element); } } else { array_unshift($result, $item); } } return $result; } $start = microtime(TRUE); for($x=0;$x<10000;$x++){ flatten1($input); }; $end = microtime(TRUE); $diff = $end-$start; $sec = intval($diff); $micro = $diff - $sec; $final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.10f', $micro)); echo "Flatten1: " . $final . "\n"; $start = microtime(TRUE); for($x=0;$x<10000;$x++){ flatten2($input); }; $end = microtime(TRUE); $diff = $end-$start; $sec = intval($diff); $micro = $diff - $sec; $final = strftime('%T', mktime(0, 0, $sec)) . str_replace('0.', '.', sprintf('%.10f', $micro)); echo "Flatten2: " . $final . "\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.3.10.0200.30216.61
7.3.00.0130.30116.73
7.2.130.0260.31916.89
7.2.120.0370.34216.77
7.2.110.0170.30816.82
7.2.100.1400.31215.51
7.2.90.2030.32915.82
7.2.80.1580.32915.73
7.2.70.1430.31216.16
7.2.60.1380.31516.06
7.2.50.1580.33115.79
7.2.40.1920.35815.72
7.2.30.1830.37615.85
7.2.20.2150.36215.72
7.2.10.2000.38315.60
7.2.00.1820.31716.08
7.1.250.0100.48115.48
7.1.220.4200.48513.87
7.1.210.3190.40213.84
7.1.200.2500.36013.61
7.1.190.3100.33113.66
7.1.180.2730.34013.97
7.1.170.3940.48913.77
7.1.160.3830.48413.74
7.1.150.3510.39014.04
7.1.140.3210.44613.80
7.1.130.3630.44113.52
7.1.120.3380.54113.88
7.1.110.3770.49713.82
7.1.100.3380.48614.16
7.1.90.4700.40913.95
7.1.80.4110.40113.89
7.1.70.4290.39413.64
7.1.60.4390.44631.96
7.1.50.6530.43131.99
7.1.40.6590.42531.40
7.1.30.6350.43932.01
7.1.20.7150.47331.80
7.1.10.5860.52613.70
7.1.00.3870.35113.99

preferences:
28.87 ms | 403 KiB | 5 Q