Last active
June 7, 2021 10:09
-
-
Save zualex/3b659eb5dcfa85d758b2d67ba674c437 to your computer and use it in GitHub Desktop.
Array vs SplFixedArray: creation time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
Result: https://docs.google.com/spreadsheets/d/1CvYtz40_HWFU_1ZMbzQQM5hykwD7hXPz8joTC8tCKgQ/edit#gid=0