<?php
$iters = 500;
$arr = range(0, 40_000);
$t = microtime(true);
foreach (range(0, $iters) as $i) {
$arr = array_merge($arr, [$i => 'x']);
}
echo round((microtime(true) - $t) * 3, 3) . ' ms' . "\n";
$t = microtime(true);
foreach (range(0, $iters) as $i) {
$arr = [...$arr, $i => 'x'];
}
echo round((microtime(true) - $t) * 3, 3) . ' ms' . "\n";
$t = microtime(true);
foreach (range(0, $iters) as $i) {
$arr = [$i => 'x', ...$arr];
}
echo round((microtime(true) - $t) * 3, 3) . ' ms' . "\n";
$t = microtime(true);
foreach (range(0, $iters) as $i) {
$arr[$i] = 'y';
}
echo round((microtime(true) - $t) * 3, 3) . ' ms' . "\n";
var_dump(count($arr)); // dummy use of $arr variable
preferences:
24.08 ms | 408 KiB | 5 Q