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; }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.30.0081.55822.96
8.2.50.0201.03819.52
8.2.40.0101.04519.52
8.2.30.0171.03219.52
8.2.20.0131.05519.52
8.2.10.0031.04919.52
8.2.00.0101.03719.52
8.1.180.0071.02519.52
8.1.170.0071.03519.52
8.1.160.0131.02219.52
8.1.150.0201.00819.52
8.1.140.0131.02419.52
8.1.130.0131.01619.52
8.1.120.0071.03519.52
8.1.110.0071.02219.52
8.1.100.0231.01219.52
8.1.90.0131.03419.52
8.1.80.0171.01619.52
8.1.70.0071.02619.52
8.1.60.0261.00919.52
8.1.50.0131.01919.52
8.1.40.0171.03019.52
8.1.30.0131.02319.52
8.1.20.0131.02219.52
8.1.10.0071.02819.52
8.1.00.0201.02219.52
8.0.280.0171.02819.52
8.0.270.0201.02119.52
8.0.260.0131.01419.52
8.0.250.0101.03619.52
8.0.240.0171.02919.52
8.0.230.0131.03019.52
8.0.220.0131.01919.52
8.0.210.0131.04519.52
8.0.200.0101.02819.52
8.0.190.0101.03419.52
8.0.180.0131.02419.52
8.0.170.0161.01819.52
8.0.160.0031.03119.52
8.0.150.0171.02219.52
8.0.140.0131.02819.52
8.0.130.0031.03319.52
8.0.120.0131.02219.52
8.0.110.0071.02819.52
8.0.100.0201.01619.52
8.0.90.0071.03319.52
8.0.80.0031.02919.52
8.0.70.0171.01619.52
8.0.60.0131.02119.52
8.0.50.0101.02319.52
8.0.30.0201.01719.52
8.0.20.0231.00719.52
8.0.10.0101.02519.52

preferences:
22.88 ms | 401 KiB | 5 Q