Skip to content

Instantly share code, notes, and snippets.

@keopx
Created July 7, 2015 07:52
Show Gist options
  • Save keopx/86ed1a28bcaa6e6bd1bf to your computer and use it in GitHub Desktop.
Save keopx/86ed1a28bcaa6e6bd1bf to your computer and use it in GitHub Desktop.
Speed Test: array_push vs $array[]
<?php
$time_start = microtime(true);
$myArray = array();
for ($i = 0; $i < 100000; ++$i) {
$myArray[] = $i;
$myArray[] = 'test a string';
}
$time_end = microtime(true);
printf("Took %f seconds for array[]\n", $time_end - $time_start);
$time_start = microtime(true);
$myArray = array();
for ($i = 0; $i < 100000; ++$i) {
array_push($myArray, $i);
array_push($myArray, 'test a string');
}
$time_end = microtime(true);
printf("Took %f seconds for array_push\n", $time_end - $time_start);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment