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 (['generator', 'construct', 'mapCombine', 'mapFlatten', 'mapUncolumn', 'walk', 'reduce'] 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.40.0130.15723.58
8.3.30.0150.15322.90
8.3.20.0150.15125.67
8.3.10.0180.16425.71
8.3.00.0180.15622.75
8.2.170.0120.15622.88
8.2.160.0130.15624.02
8.2.150.0100.14623.17
8.2.140.0130.16924.29
8.2.130.0180.16024.50
8.2.120.0180.15523.30
8.2.110.0170.15723.20
8.2.100.0150.16123.29
8.2.90.0200.13822.82
8.2.80.0100.17323.15
8.2.70.0170.14622.81
8.2.60.0180.15622.86
8.2.50.0150.14523.03
8.2.40.0230.14522.84
8.2.30.0150.13922.72
8.2.20.0150.14922.66
8.2.10.0170.15222.64
8.2.00.0050.15122.83
8.1.270.0130.15325.81
8.1.260.0070.16425.72
8.1.250.0120.14725.84
8.1.240.0220.14122.24
8.1.230.0170.15722.11
8.1.220.0120.15422.53
8.1.210.0180.14522.28
8.1.200.0180.16822.43
8.1.190.0070.14822.35
8.1.180.0100.14822.31
8.1.170.0130.13722.33
8.1.160.0180.15822.13
8.1.150.0100.14922.25
8.1.140.0120.15722.40
8.1.130.0070.14522.52
8.1.120.0170.15022.21
8.1.110.0200.15822.06
8.1.100.0200.14223.40
8.1.90.0180.16022.13
8.1.80.0200.14623.29
8.1.70.0200.13322.40
8.1.60.0200.15322.34
8.1.50.0150.15922.15
8.1.40.0150.15322.33
8.1.30.0230.15922.61
8.1.20.0180.14923.71
8.1.10.0120.15222.17
8.1.00.0170.15322.10

preferences:
27.09 ms | 403 KiB | 5 Q