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 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, 5000); foreach (['generatorPrefix', 'constructPrefix', 'mapKeyAndCombinePrefix', 'walkPrefix', 'reducePrefix'] as $test) { echo "Duration of $test: ", returnTime(fn() => $test($array)) . PHP_EOL; }
Output for git.master
Duration of generatorPrefix: 0.56370496749878 Duration of constructPrefix: 0.37879943847656 Duration of mapKeyAndCombinePrefix: 0.55135488510132 Duration of walkPrefix: 0.79895257949829 Duration of reducePrefix: 49.132585525513
Output for git.master_jit
Duration of generatorPrefix: 0.56980848312378 Duration of constructPrefix: 0.36269426345825 Duration of mapKeyAndCombinePrefix: 0.56769847869873 Duration of walkPrefix: 0.81355571746826 Duration of reducePrefix: 49.228310585022

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:
17.7 ms | 401 KiB | 7 Q