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 substrReplaceCombine($array) { return array_combine( substr_replace($array, 'key', 0, 0), substr_replace($array, 'value', 0, 0) ); } 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 (['generator', 'construct', 'substrReplaceCombine', 'mapCombine', 'mapFlatten', 'mapUncolumn', 'walk', 'reduce'] as $test) { echo "Duration of $test: ", returnTime(fn() => $test($array)) . PHP_EOL; }
Output for git.master_jit
Duration of generator: 0.93539953231812 Duration of construct: 0.69663524627686 Duration of substrReplaceCombine: 0.73616504669189 Duration of mapCombine: 0.94695091247559 Duration of mapFlatten: 1.1117935180664 Duration of mapUncolumn: 1.1517643928528 Duration of walk: 1.1464715003967 Duration of reduce: 0.92320442199707
Output for git.master
Duration of generator: 0.98052024841309 Duration of construct: 0.69215297698975 Duration of substrReplaceCombine: 0.74890851974487 Duration of mapCombine: 0.93257427215576 Duration of mapFlatten: 1.14825963974 Duration of mapUncolumn: 1.142156124115 Duration of walk: 1.1546969413757 Duration of reduce: 0.95226764678955
Output for rfc.property-hooks
Duration of generator: 1.4171838760376 Duration of construct: 0.72499513626099 Duration of substrReplaceCombine: 0.85246562957764 Duration of mapCombine: 1.0023713111877 Duration of mapFlatten: 1.2841463088989 Duration of mapUncolumn: 1.1592626571655 Duration of walk: 1.1334538459778 Duration of reduce: 0.91099739074707

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
55.18 ms | 409 KiB | 5 Q