I'm seeing a huge performance decline in a (command line) PHP script, caused by a simple assignment (runtime increase from 0.8 ~ 0.9 seconds to 29.x seconds).
The script first fetches a lot of data from a MySQL database and creates objects of different custom classes. After this fetching (php now uses around 500 MB of RAM) I loop an array of approximately 3'500 Sample
objects, each of which has an associative array (size around 100 entries) as one of its properties. This array holds Value
objects, which are small objects with two properties, and the keys are integers smaller than 6'000. This is where I stumbled upon the problem, see this code:
foreach ($samples as $id => $s) { # $s is now a 'Sample' object
$values = $s->values(); # $values is an array of 'Value' objects
if (isset($values[$match_id])) {
$num_tested++;
# $val = $values[$match_id]; # contains a 'Value' object
# $val = &$values[...]; -> the loop never ends (!)
}
}
Note that commented line. If I run the code as it appears here, this block runs for about 0.8 to 0.9 seconds. If I uncomment this single line, the block runs for almost 30 seconds. I found that if the array is non-associative (it only contains consecutive keys from 0 to about 100) the runtime only increases to 1.8 ~ 1.9 seconds.
It seems that this happens because of the non-consecutive array keys I use, but then again why does the performance not already decline by calling isset($values[$match_id])
? Is there a workaround for this or do I have to live with that?
Running PHP 5.3.0, Zend Engine v2.3.0, Mac OS X Server 10.6.2