Skip to content

Instantly share code, notes, and snippets.

@zualex
Last active June 7, 2021 10:09
Show Gist options
  • Save zualex/3b659eb5dcfa85d758b2d67ba674c437 to your computer and use it in GitHub Desktop.
Save zualex/3b659eb5dcfa85d758b2d67ba674c437 to your computer and use it in GitHub Desktop.
Array vs SplFixedArray: creation time
<?php
$sizes = [];
for ($exp = 0; $exp < 10; $exp++) {
$sizes[] = pow(2, $exp) * 1000;
}
echo "size, timeArray, timeSpl" . PHP_EOL;
foreach ($sizes as $size) {
$startTime = microtime(true);
createOrdinaryArray($size);
$stopTimeOrdinary = (microtime(true) - $startTime) * 1000;
$startTime = microtime(true);
createSplArray($size);
$stopTimeSpl = (microtime(true) - $startTime) * 1000;
echo "{$size}, {$stopTimeOrdinary}, {$stopTimeSpl}" . PHP_EOL;
}
function createOrdinaryArray(int $size)
{
$array = [];
for ($i = 0; $i < $size; $i++) {
$array[$i] = null;
}
}
function createSplArray(int $size)
{
$array = new SplFixedArray($size);
for ($i = 0; $i < $size; $i++) {
$array[$i] = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment