3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getBigArray($n){ $arr = []; for ($i=0; $i<$n; $i++) { $arr[] = $i; } return $arr; } function testSpeed1($n){ $array1 = getBigArray($n); $array2 = getBigArray($n); $new = []; $start = microtime(true); testArrayManipulation1($array1, $array2); $new[] = $array1; $end = microtime(true); return $end-$start; } function testSpeed2($n){ $array1 = getBigArray($n); $array2 = getBigArray($n); $new = []; $start = microtime(true); $new[] = testArrayManipulation2($array1, $array2); $end = microtime(true); return $end-$start; } function testSpeed3($n){ $array1 = getBigArray($n); $array2 = getBigArray($n); $new = []; $start = microtime(true); $new[] = testArrayManipulation3($array1, $array2); $end = microtime(true); return $end-$start; } function testArrayManipulation1(&$array1, $array2){ foreach($array2 as $v){ $array1[] = $v; } } function testArrayManipulation2(&$array1, $array2){ foreach($array2 as $v){ $array1[] = $v; } return $array1; } function testArrayManipulation3($array1, $array2){ foreach($array2 as $v){ $array1[] = $v; } return $array1; } $n = 10; $test1 = []; $test2 = []; $test3 = []; for ($i=0; $i<$n; $i++) { $length = 100000; $test1[] = testSpeed1($length); $test2[] = testSpeed2($length); $test3[] = testSpeed3($length); } echo array_sum($test1)/count($test1); echo '<br>'; echo array_sum($test2)/count($test2); echo '<br>'; echo array_sum($test3)/count($test3);

preferences:
31.04 ms | 405 KiB | 5 Q