3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generate($array) { // 1 loop over data foreach ($array as $v) { yield 'key' . $v => 'value' . $v; } } function generator($array) { // 1 parent loop over data return iterator_to_array(generate($array)); } function construct($array) { // 1 loop over data $result = []; foreach ($array as $v) { $result['key' . $v] = 'value' . $v; } return $result; } function mapCombine($array) { // 3 loops over data return array_combine( array_map( fn($v) => 'key' . $v, $array ), array_map( fn($v) => 'value' . $v, $array ), ); } function mapFlatten($array) { // 3 loops over data return array_merge( ...array_map( fn($v) => ['key' . $v => 'value' . $v], $array ), ); } function mapUncolumn($array) { // 2 loops over data return array_column( array_map( fn($v) => ['key' . $v, 'value' . $v], $array ), 1, 0 ); } function walk($array) { // 1 loop over data $result = []; array_walk( $array, function($v) use (&$result) { $result['key' . $v] = 'value' . $v; } ); return $result; } function reduce($array) { // 1 loop over data return array_reduce( $array, function($result, $v) { $result['key' . $v] = 'value' . $v; return $result; }, new ArrayObject() ); } function returnTime(callable $function, int $repeat = 20) { $tests = []; for ($i = 0; $i < $repeat; ++$i) { $startTime = microtime(true); $function(); $endTime = microtime(true); $tests[] = $endTime - $startTime; } // Representing the average return 1000 * array_sum($tests) / $repeat; } $array = range(0, 5000); foreach (['generate', 'construct', 'mapCombine', 'mapFlatten', 'mapUncolumn', 'walk', 'reduce'] as $test) { echo "Duration of $test: ", returnTime(fn() => $test($array)) . PHP_EOL; }
Output for 8.3.4
Duration of generate: 0.00071525573730469 Duration of construct: 0.8031964302063 Duration of mapCombine: 1.0213017463684 Duration of mapFlatten: 1.219916343689 Duration of mapUncolumn: 1.3207793235779 Duration of walk: 1.2843608856201 Duration of reduce: 1.0416030883789
Output for 8.3.3
Duration of generate: 0.00088214874267578 Duration of construct: 1.2231349945068 Duration of mapCombine: 1.3159513473511 Duration of mapFlatten: 1.5614032745361 Duration of mapUncolumn: 1.4692544937134 Duration of walk: 1.2946963310242 Duration of reduce: 1.0288953781128
Output for 8.3.2
Duration of generate: 0.00070333480834961 Duration of construct: 0.88691711425781 Duration of mapCombine: 1.2844920158386 Duration of mapFlatten: 1.3662338256836 Duration of mapUncolumn: 1.3576626777649 Duration of walk: 1.4884352684021 Duration of reduce: 1.1671543121338
Output for 8.3.1
Duration of generate: 0.0006556510925293 Duration of construct: 0.8487343788147 Duration of mapCombine: 1.0464191436768 Duration of mapFlatten: 1.2350797653198 Duration of mapUncolumn: 1.2910604476929 Duration of walk: 1.3036370277405 Duration of reduce: 1.1544704437256
Output for 8.3.0
Duration of generate: 0.00060796737670898 Duration of construct: 0.88683366775513 Duration of mapCombine: 1.27272605896 Duration of mapFlatten: 1.6091465950012 Duration of mapUncolumn: 1.3442039489746 Duration of walk: 1.300585269928 Duration of reduce: 0.99948644638062
Output for 8.2.17
Duration of generate: 0.0006556510925293 Duration of construct: 0.82064867019653 Duration of mapCombine: 0.99749565124512 Duration of mapFlatten: 1.5724420547485 Duration of mapUncolumn: 1.1867523193359 Duration of walk: 1.2895822525024 Duration of reduce: 1.0228157043457
Output for 8.2.16
Duration of generate: 0.0006556510925293 Duration of construct: 0.78954696655273 Duration of mapCombine: 0.98870992660522 Duration of mapFlatten: 1.2372016906738 Duration of mapUncolumn: 1.2055993080139 Duration of walk: 1.2340903282166 Duration of reduce: 0.96981525421143
Output for 8.2.15
Duration of generate: 0.0006556510925293 Duration of construct: 0.84733963012695 Duration of mapCombine: 1.0493755340576 Duration of mapFlatten: 1.2033462524414 Duration of mapUncolumn: 1.2435555458069 Duration of walk: 1.2323975563049 Duration of reduce: 0.99421739578247
Output for 8.2.14
Duration of generate: 0.00060796737670898 Duration of construct: 0.85846185684204 Duration of mapCombine: 1.0532140731812 Duration of mapFlatten: 1.2762784957886 Duration of mapUncolumn: 1.3206839561462 Duration of walk: 1.2653231620789 Duration of reduce: 1.0496497154236
Output for 8.2.13
Duration of generate: 0.00045299530029297 Duration of construct: 0.85666179656982 Duration of mapCombine: 1.0541439056396 Duration of mapFlatten: 1.2078046798706 Duration of mapUncolumn: 1.1982321739197 Duration of walk: 1.3153672218323 Duration of reduce: 0.99080801010132
Output for 8.2.12
Duration of generate: 0.00045299530029297 Duration of construct: 0.82625150680542 Duration of mapCombine: 1.1025309562683 Duration of mapFlatten: 1.2571573257446 Duration of mapUncolumn: 1.2722134590149 Duration of walk: 1.2493491172791 Duration of reduce: 1.0213375091553
Output for 8.2.11
Duration of generate: 0.00059604644775391 Duration of construct: 0.78779458999634 Duration of mapCombine: 1.0303378105164 Duration of mapFlatten: 1.2040138244629 Duration of mapUncolumn: 1.2179851531982 Duration of walk: 1.2306332588196 Duration of reduce: 0.99195241928101
Output for 8.2.10
Duration of generate: 0.00059604644775391 Duration of construct: 0.80873966217041 Duration of mapCombine: 1.0424017906189 Duration of mapFlatten: 1.2081623077393 Duration of mapUncolumn: 1.2938380241394 Duration of walk: 1.1927127838135 Duration of reduce: 0.97600221633911
Output for 8.2.9
Duration of generate: 0.00064373016357422 Duration of construct: 0.78381299972534 Duration of mapCombine: 0.98655223846436 Duration of mapFlatten: 1.2054681777954 Duration of mapUncolumn: 1.224422454834 Duration of walk: 1.3800144195557 Duration of reduce: 1.178514957428
Output for 8.2.8
Duration of generate: 0.00058412551879883 Duration of construct: 0.96069574356079 Duration of mapCombine: 0.98775625228882 Duration of mapFlatten: 1.2418031692505 Duration of mapUncolumn: 1.1884570121765 Duration of walk: 1.2320041656494 Duration of reduce: 0.96030235290527
Output for 8.2.7
Duration of generate: 0.00070333480834961 Duration of construct: 0.8423924446106 Duration of mapCombine: 0.97675323486328 Duration of mapFlatten: 1.3129830360413 Duration of mapUncolumn: 1.2647151947021 Duration of walk: 1.2595534324646 Duration of reduce: 0.98297595977783
Output for 8.2.6
Duration of generate: 0.0006556510925293 Duration of construct: 0.81750154495239 Duration of mapCombine: 0.99596977233887 Duration of mapFlatten: 1.2103080749512 Duration of mapUncolumn: 1.2637495994568 Duration of walk: 1.2544631958008 Duration of reduce: 0.99978446960449
Output for 8.2.5
Duration of generate: 0.00056028366088867 Duration of construct: 0.77996253967285 Duration of mapCombine: 1.0098814964294 Duration of mapFlatten: 1.2006402015686 Duration of mapUncolumn: 1.2919068336487 Duration of walk: 1.3202548027039 Duration of reduce: 0.96725225448608
Output for 8.2.4
Duration of generate: 0.00070333480834961 Duration of construct: 0.78316926956177 Duration of mapCombine: 0.97471475601196 Duration of mapFlatten: 1.2144565582275 Duration of mapUncolumn: 1.4443755149841 Duration of walk: 1.2081861495972 Duration of reduce: 0.98874568939209
Output for 8.2.3
Duration of generate: 0.00054836273193359 Duration of construct: 0.79026222229004 Duration of mapCombine: 0.97984075546265 Duration of mapFlatten: 1.2137770652771 Duration of mapUncolumn: 1.3170838356018 Duration of walk: 1.4739871025085 Duration of reduce: 1.3666152954102
Output for 8.2.2
Duration of generate: 0.0006556510925293 Duration of construct: 0.82986354827881 Duration of mapCombine: 1.0543346405029 Duration of mapFlatten: 1.2328505516052 Duration of mapUncolumn: 1.2429356575012 Duration of walk: 1.298713684082 Duration of reduce: 0.97715854644775
Output for 8.2.1
Duration of generate: 0.0006556510925293 Duration of construct: 0.82833766937256 Duration of mapCombine: 1.0447978973389 Duration of mapFlatten: 1.227605342865 Duration of mapUncolumn: 1.2042880058289 Duration of walk: 1.2755751609802 Duration of reduce: 1.0010957717896
Output for 8.2.0
Duration of generate: 0.0004887580871582 Duration of construct: 0.77360868453979 Duration of mapCombine: 1.0326385498047 Duration of mapFlatten: 1.1968016624451 Duration of mapUncolumn: 1.2157320976257 Duration of walk: 1.20689868927 Duration of reduce: 0.96701383590698
Output for 8.1.27
Duration of generate: 0.00039339065551758 Duration of construct: 0.74949264526367 Duration of mapCombine: 1.0092616081238 Duration of mapFlatten: 1.1675000190735 Duration of mapUncolumn: 1.1167526245117 Duration of walk: 1.3001084327698 Duration of reduce: 0.95945596694946
Output for 8.1.26
Duration of generate: 0.00056028366088867 Duration of construct: 1.0655522346497 Duration of mapCombine: 1.2265086174011 Duration of mapFlatten: 1.2807488441467 Duration of mapUncolumn: 1.1898875236511 Duration of walk: 1.3988733291626 Duration of reduce: 1.0579705238342
Output for 8.1.25
Duration of generate: 0.00050067901611328 Duration of construct: 0.76920986175537 Duration of mapCombine: 1.0122418403625 Duration of mapFlatten: 1.3691902160645 Duration of mapUncolumn: 1.1752605438232 Duration of walk: 1.4146566390991 Duration of reduce: 1.1165499687195
Output for 8.1.24
Duration of generate: 0.00075101852416992 Duration of construct: 0.90051889419556 Duration of mapCombine: 1.0282278060913 Duration of mapFlatten: 1.1305570602417 Duration of mapUncolumn: 1.2671232223511 Duration of walk: 1.7375230789185 Duration of reduce: 1.1731386184692
Output for 8.1.23
Duration of generate: 0.00079870223999023 Duration of construct: 0.81299543380737 Duration of mapCombine: 1.0611057281494 Duration of mapFlatten: 1.2178778648376 Duration of mapUncolumn: 1.2150764465332 Duration of walk: 1.4270901679993 Duration of reduce: 1.0360598564148
Output for 8.1.22
Duration of generate: 0.00044107437133789 Duration of construct: 0.80835819244385 Duration of mapCombine: 1.0160565376282 Duration of mapFlatten: 1.2693405151367 Duration of mapUncolumn: 1.1243581771851 Duration of walk: 1.3817667961121 Duration of reduce: 1.0233163833618
Output for 8.1.21
Duration of generate: 0.0010013580322266 Duration of construct: 0.84223747253418 Duration of mapCombine: 1.0451316833496 Duration of mapFlatten: 1.1807560920715 Duration of mapUncolumn: 1.1099696159363 Duration of walk: 1.3459801673889 Duration of reduce: 0.98246335983276
Output for 8.1.20
Duration of generate: 0.00081062316894531 Duration of construct: 0.80937147140503 Duration of mapCombine: 1.028311252594 Duration of mapFlatten: 1.2259364128113 Duration of mapUncolumn: 1.1401057243347 Duration of walk: 1.3705968856812 Duration of reduce: 1.0074734687805
Output for 8.1.19
Duration of generate: 0.1448392868042 Duration of construct: 0.77600479125977 Duration of mapCombine: 1.0143518447876 Duration of mapFlatten: 1.1807680130005 Duration of mapUncolumn: 1.1340141296387 Duration of walk: 1.6902923583984 Duration of reduce: 1.4151930809021
Output for 8.1.18
Duration of generate: 0.0010967254638672 Duration of construct: 0.73834657669067 Duration of mapCombine: 1.0213136672974 Duration of mapFlatten: 1.2072324752808 Duration of mapUncolumn: 1.2057065963745 Duration of walk: 1.360547542572 Duration of reduce: 1.0272622108459
Output for 8.1.17
Duration of generate: 0.12401342391968 Duration of construct: 0.8373498916626 Duration of mapCombine: 0.99586248397827 Duration of mapFlatten: 1.2099981307983 Duration of mapUncolumn: 1.1683344841003 Duration of walk: 1.4155983924866 Duration of reduce: 1.0090351104736
Output for 8.1.16
Duration of generate: 0.00070333480834961 Duration of construct: 0.77588558197021 Duration of mapCombine: 1.0597586631775 Duration of mapFlatten: 1.1320829391479 Duration of mapUncolumn: 1.1297941207886 Duration of walk: 1.3292789459229 Duration of reduce: 1.010000705719
Output for 8.1.15
Duration of generate: 0.12810230255127 Duration of construct: 0.79593658447266 Duration of mapCombine: 1.0698199272156 Duration of mapFlatten: 1.1758327484131 Duration of mapUncolumn: 1.1049151420593 Duration of walk: 1.3504505157471 Duration of reduce: 0.98841190338135
Output for 8.1.14
Duration of generate: 0.11910200119019 Duration of construct: 0.78506469726562 Duration of mapCombine: 1.0202646255493 Duration of mapFlatten: 1.2822508811951 Duration of mapUncolumn: 1.4455556869507 Duration of walk: 1.5799403190613 Duration of reduce: 1.0509729385376
Output for 8.1.13
Duration of generate: 0.15380382537842 Duration of construct: 1.1264085769653 Duration of mapCombine: 1.7332553863525 Duration of mapFlatten: 1.9083499908447 Duration of mapUncolumn: 1.9654631614685 Duration of walk: 1.9844532012939 Duration of reduce: 1.5456438064575
Output for 8.1.12
Duration of generate: 0.00079870223999023 Duration of construct: 1.1786818504333 Duration of mapCombine: 1.5159606933594 Duration of mapFlatten: 1.2301206588745 Duration of mapUncolumn: 1.200258731842 Duration of walk: 1.5419960021973 Duration of reduce: 1.0288596153259
Output for 8.1.11
Duration of generate: 0.000762939453125 Duration of construct: 1.0662794113159 Duration of mapCombine: 1.0518908500671 Duration of mapFlatten: 1.2289762496948 Duration of mapUncolumn: 1.1433720588684 Duration of walk: 1.3532161712646 Duration of reduce: 1.0353088378906
Output for 8.1.10
Duration of generate: 0.00069141387939453 Duration of construct: 0.81914663314819 Duration of mapCombine: 1.0637283325195 Duration of mapFlatten: 1.2192130088806 Duration of mapUncolumn: 1.1517763137817 Duration of walk: 1.3339519500732 Duration of reduce: 0.96666812896729
Output for 8.1.9
Duration of generate: 0.00070333480834961 Duration of construct: 0.87224245071411 Duration of mapCombine: 1.0263085365295 Duration of mapFlatten: 1.2433528900146 Duration of mapUncolumn: 1.1639595031738 Duration of walk: 1.3467907905579 Duration of reduce: 0.98143815994263
Output for 8.1.8
Duration of generate: 0.00064373016357422 Duration of construct: 0.79910755157471 Duration of mapCombine: 1.0762214660645 Duration of mapFlatten: 1.2539386749268 Duration of mapUncolumn: 1.200532913208 Duration of walk: 1.359236240387 Duration of reduce: 1.0231852531433
Output for 8.1.7
Duration of generate: 0.18255710601807 Duration of construct: 0.76166391372681 Duration of mapCombine: 1.0432481765747 Duration of mapFlatten: 1.1507749557495 Duration of mapUncolumn: 1.1221170425415 Duration of walk: 1.3854622840881 Duration of reduce: 1.015830039978
Output for 8.1.6
Duration of generate: 0.00064373016357422 Duration of construct: 1.0445952415466 Duration of mapCombine: 1.0511517524719 Duration of mapFlatten: 1.1935234069824 Duration of mapUncolumn: 1.1297821998596 Duration of walk: 1.3783693313599 Duration of reduce: 0.98799467086792
Output for 8.1.5
Duration of generate: 0.00054836273193359 Duration of construct: 0.75271129608154 Duration of mapCombine: 0.99546909332275 Duration of mapFlatten: 1.1698007583618 Duration of mapUncolumn: 1.1950731277466 Duration of walk: 1.4015436172485 Duration of reduce: 0.99564790725708
Output for 8.1.4
Duration of generate: 0.00059604644775391 Duration of construct: 0.76240301132202 Duration of mapCombine: 1.0566115379333 Duration of mapFlatten: 1.2427568435669 Duration of mapUncolumn: 1.1776089668274 Duration of walk: 1.3355612754822 Duration of reduce: 1.0140895843506
Output for 8.1.3
Duration of generate: 0.00060796737670898 Duration of construct: 0.89221000671387 Duration of mapCombine: 1.1019468307495 Duration of mapFlatten: 1.315975189209 Duration of mapUncolumn: 1.1481165885925 Duration of walk: 1.3145446777344 Duration of reduce: 1.0358452796936
Output for 8.1.2
Duration of generate: 0.0006556510925293 Duration of construct: 0.82670450210571 Duration of mapCombine: 1.0580539703369 Duration of mapFlatten: 1.2312173843384 Duration of mapUncolumn: 1.1786103248596 Duration of walk: 1.3314008712769 Duration of reduce: 1.0245442390442
Output for 8.1.1
Duration of generate: 0.00060796737670898 Duration of construct: 0.82082748413086 Duration of mapCombine: 1.021134853363 Duration of mapFlatten: 1.1842012405396 Duration of mapUncolumn: 1.1052966117859 Duration of walk: 1.4307498931885 Duration of reduce: 1.0219097137451
Output for 8.1.0
Duration of generate: 0.00050067901611328 Duration of construct: 0.79765319824219 Duration of mapCombine: 1.2101531028748 Duration of mapFlatten: 1.4287829399109 Duration of mapUncolumn: 1.4651536941528 Duration of walk: 1.5340924263 Duration of reduce: 1.0066390037537

preferences:
84.25 ms | 402 KiB | 56 Q