Revision: 801
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 9, 2006 09:27 by mthorn
Initial Code
$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);
Initial URL
http://www.mthorn.net
Initial Description
Took 0.164692 seconds for array[] Took 0.395778 seconds for array_push As you can see array_push is more than twice as slow. Here are the reasons. Array_push is a function call, Function calls are always slower. Array_push takes mixed parameters, parameter checking is always slower. Also array[] just looks cleaner and is less to type. Tips: Always pre-intialize your variables and don't use mixed types even though you can.
Initial Title
Speed Test: array_push vs $array[]
Initial Tags
php, array
Initial Language
PHP