3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generator($array, $prefix) { foreach ($array as $k => $v) { yield $prefix . $k => $v; } } function generatorPrefix($array, $prefix = 'prefix') { return iterator_to_array(generator($array, $prefix)); } function constructPrefix($array, $prefix = 'prefix') { // 1 loop over data $result = []; foreach ($array as $k => $v) { $result[$prefix . $k] = $v; } return $result; } function mapKeyAndCombinePrefix($array, $prefix = 'prefix') { // 3 loops over data return array_combine( array_map( fn($k) => $prefix . $k, array_keys($array) ), $array ); } function substrReplaceAndCombinePrefix($array, $prefix = 'prefix') { // 3 loops over data return array_combine( substr_replace(array_keys($array), $prefix, 0, 0), $array ); } function walkPrefix($array, $prefix = 'prefix') { // 1 loop over data $result = []; array_walk( $array, function($v, $k, $prefix) use (&$result) { $result[$prefix . $k] = $v; }, $prefix ); return $result; } function reducePrefix($array, $prefix = 'prefix') { // 2 loops over data return array_reduce( array_keys($array), function($result, $k) use ($array, $prefix) { $result[$prefix . $k] = $array[$k]; return $result; }, [] ); } 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, 2000); foreach (['substrReplaceAndCombinePrefix', 'constructPrefix', 'mapKeyAndCombinePrefix', 'generatorPrefix', 'walkPrefix', 'reducePrefix'] as $test) { echo "Duration of $test: ", returnTime(fn() => $test($array)) . PHP_EOL; }
Output for git.master_jit
Duration of substrReplaceAndCombinePrefix: 0.18355846405029 Duration of constructPrefix: 0.17215013504028 Duration of mapKeyAndCombinePrefix: 0.23632049560547 Duration of generatorPrefix: 0.25166273117065 Duration of walkPrefix: 0.32198429107666 Duration of reducePrefix: 11.581778526306
Output for git.master
Duration of substrReplaceAndCombinePrefix: 0.16965866088867 Duration of constructPrefix: 0.1738429069519 Duration of mapKeyAndCombinePrefix: 0.22979974746704 Duration of generatorPrefix: 0.25275945663452 Duration of walkPrefix: 0.3203272819519 Duration of reducePrefix: 11.507129669189
Output for rfc.property-hooks
Duration of substrReplaceAndCombinePrefix: 0.18290281295776 Duration of constructPrefix: 0.17499923706055 Duration of mapKeyAndCombinePrefix: 0.22473335266113 Duration of generatorPrefix: 0.24400949478149 Duration of walkPrefix: 0.31160116195679 Duration of reducePrefix: 11.333823204041

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:
34.79 ms | 409 KiB | 5 Q