Bug #13607: Adding to array elements - performance problem
| From: | webmaster at vettweb dot net dot au | Date: | Tue, 09 Oct 2001 04:57:02 +0000 |
| Subject: | Bug #13607: Adding to array elements - performance problem | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-67578@lists.php.net to get a copy of this message | ||
From: webmaster@vettweb.net.au
Operating system: Win2000 (NT 5.0 build 2195)
PHP version: 4.0.5
PHP Bug Type: Arrays related
Bug description: Adding to array elements - performance problem
I have a php script that displays a report. The report is based on data
retrieved from a database (MySQL in this case but can be alomst any).
I am doing some summation on the data retrieved.
I was attempting to add values to the array and found that my script was
running fairly slow (30 seconds to process 89 records)
Through alot of trial and error I determined the following problem.
If you have an empty array
i.e. $somearray = array();
and you try to add a value to an element
i.e. $somearray[0] += 100;
it takes considerably longer for php to do this than if we have the
following example
$somearray = array(0);
$somearray[0] += 100;
It appears that php has some problems adding to a value in an array if the
item does not already exist.
By Initialising my arrays in my script to 0 I cut the execution time for
the script from 30 seconds to 4 seconds. This script was only trying to
add into none existant array elements 120 times in my script and yet it
took almost 26 seconds to do this.
If this is not a bug it should atleast be brought to peoples attention that
this can cause performance problems in some situation.
Here is a complete example script with timer
<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
// Create the array
//
$add_test = array();
// Start the first pass timer
//
$firstpassstart = getmicrotime();
// run through the array the first time
//
for ($loopcounter = 0; $loopcounter <= 100; $loopcounter++) {
// Add 1 to each element
// Note none of these elements exist
$add_test[$loopcounter] += 100000;
}
// Stop the first pass timer
//
$firstpassfinish = getmicrotime();
$firstpass = ($firstpassfinish - $firstpassstart);
// Start the Second Pass timer
//
$secondpassstart = getmicrotime();
// run through the array a second time
//
for ($loopcounter = 0; $loopcounter <= 100; $loopcounter++) {
// Add 1 to each element
// Note these elements all exist
$add_test[$loopcounter] += 100000;
}
// Stop the Second Pass timer
//
$secondpassfinish = getmicrotime();
$secondpass = ($secondpassfinish - $secondpassstart);
// Display the results
//
echo "First Pass took $firstpass seconds<br>\n";
echo "Second Pass took $secondpass seconds<br>\n";
?>
When I run this the first pass is 5 times slower than the second pass.
While the times I get for this script are small (First pass takes 0.004
seconds) in some situations this can be much worse.
--
Edit bug report at: http://bugs.php.net/?id=13607&edit=1