<?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";
- Output for 7.3.1
- Flatten1: 00:00:00.1218681335
Flatten2: 00:00:00.1676180363
- Output for 7.3.0
- Flatten1: 00:00:00.1382639408
Flatten2: 00:00:00.1592721939
- Output for 7.2.13
- Flatten1: 00:00:00.1412570477
Flatten2: 00:00:00.1796867847
- Output for 7.2.12
- Flatten1: 00:00:00.1461391449
Flatten2: 00:00:00.1815979481
- Output for 7.2.11
- Flatten1: 00:00:00.1329901218
Flatten2: 00:00:00.1693010330
- Output for 7.2.10
- Flatten1: 00:00:00.1722719669
Flatten2: 00:00:00.1955020428
- Output for 7.2.9
- Flatten1: 00:00:00.2071330547
Flatten2: 00:00:00.1891889572
- Output for 7.2.8
- Flatten1: 00:00:00.1478579044
Flatten2: 00:00:00.1829779148
- Output for 7.2.7
- Flatten1: 00:00:00.1408050060
Flatten2: 00:00:00.2027568817
- Output for 7.2.6
- Flatten1: 00:00:00.1707170010
Flatten2: 00:00:00.1753058434
- Output for 7.2.5
- Flatten1: 00:00:00.1542778015
Flatten2: 00:00:00.1860589981
- Output for 7.2.4
- Flatten1: 00:00:00.1825890541
Flatten2: 00:00:00.1728758812
- Output for 7.2.3
- Flatten1: 00:00:00.1771650314
Flatten2: 00:00:00.1878070831
- Output for 7.2.2
- Flatten1: 00:00:00.1753709316
Flatten2: 00:00:00.1685929298
- Output for 7.2.1
- Flatten1: 00:00:00.1567478180
Flatten2: 00:00:00.1930580139
- Output for 7.2.0
- Flatten1: 00:00:00.1504859924
Flatten2: 00:00:00.1810131073
- Output for 7.1.25
- Flatten1: 00:00:00.2051239014
Flatten2: 00:00:00.2696809769
- Output for 7.1.22
- Flatten1: 00:00:00.1586589813
Flatten2: 00:00:00.3447279930
- Output for 7.1.21
- Flatten1: 00:00:00.1460139751
Flatten2: 00:00:00.2691249847
- Output for 7.1.20
- Flatten1: 00:00:00.1599559784
Flatten2: 00:00:00.2017300129
- Output for 7.1.19
- Flatten1: 00:00:00.1527910233
Flatten2: 00:00:00.1880810261
- Output for 7.1.18
- Flatten1: 00:00:00.1468329430
Flatten2: 00:00:00.1969552040
- Output for 7.1.17
- Flatten1: 00:00:00.2356338501
Flatten2: 00:00:00.2896909714
- Output for 7.1.16
- Flatten1: 00:00:00.2399458885
Flatten2: 00:00:00.2960639000
- Output for 7.1.15
- Flatten1: 00:00:00.1567201614
Flatten2: 00:00:00.2318921089
- Output for 7.1.14
- Flatten1: 00:00:00.2174091339
Flatten2: 00:00:00.2360801697
- Output for 7.1.13
- Flatten1: 00:00:00.1826190948
Flatten2: 00:00:00.2681879997
- Output for 7.1.12
- Flatten1: 00:00:00.2307820320
Flatten2: 00:00:00.3108010292
- Output for 7.1.11
- Flatten1: 00:00:00.1987218857
Flatten2: 00:00:00.2965950966
- Output for 7.1.10
- Flatten1: 00:00:00.1968760490
Flatten2: 00:00:00.3138840199
- Output for 7.1.9
- Flatten1: 00:00:00.1739509106
Flatten2: 00:00:00.2451181412
- Output for 7.1.8
- Flatten1: 00:00:00.1630239487
Flatten2: 00:00:00.2373518944
- Output for 7.1.7
- Flatten1: 00:00:00.1659259796
Flatten2: 00:00:00.2366809845
- Output for 7.1.6
- Flatten1: 00:00:00.2369258404
Flatten2: 00:00:00.2135608196
- Output for 7.1.5
- Flatten1: 00:00:00.1625330448
Flatten2: 00:00:00.2725410461
- Output for 7.1.4
- Flatten1: 00:00:00.1962909698
Flatten2: 00:00:00.2313749790
- Output for 7.1.3
- Flatten1: 00:00:00.1777579784
Flatten2: 00:00:00.2720499039
- Output for 7.1.2
- Flatten1: 00:00:00.2041718960
Flatten2: 00:00:00.2751619816
- Output for 7.1.1
- Flatten1: 00:00:00.2252819538
Flatten2: 00:00:00.3047490120
- Output for 7.1.0
- Flatten1: 00:00:00.1390779018
Flatten2: 00:00:00.2198760509
preferences:
46.59 ms | 460 KiB | 5 Q