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; }
Output for git.master
Duration of generator: 1.0839104652405 Duration of construct: 1.0250091552734 Duration of mapCombine: 1.4595866203308 Duration of mapFlatten: 1.5263199806213 Duration of mapUncolumn: 1.1833071708679 Duration of walk: 1.1772751808167 Duration of reduce: 0.93740224838257
Output for git.master_jit
Duration of generator: 1.5579581260681 Duration of construct: 0.7115364074707 Duration of mapCombine: 0.97376108169556 Duration of mapFlatten: 1.1563181877136 Duration of mapUncolumn: 1.1124014854431 Duration of walk: 1.2271523475647 Duration of reduce: 0.91574192047119
Output for rfc.property-hooks
Duration of generator: 1.4291286468506 Duration of construct: 1.0303258895874 Duration of mapCombine: 1.0749101638794 Duration of mapFlatten: 1.1502146720886 Duration of mapUncolumn: 1.1770844459534 Duration of walk: 1.1919498443604 Duration of reduce: 1.3106465339661

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