|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2000-09-07 13:01 UTC] mledet at spirenet dot com
The following code performed well under 3.0.16.
for ($i=0; $i < sizeof($teacherarray); $i++) {
$a=$teacher_array[$i];
if ($a->teacher_id==$teacher_id) {
break;
}
}
However, under php 4.0.2 it is extrememly slow and causes cpu util for the httpd process to hit 90%. $teacherarray is not being modified during the loop so the condition isn't changing.
BTW: Changing to the following code works ok.
$numteachers = sizeof($teacherarray);
for ($i=0; $i < $numteachers; $i++) {
$a=$teacher_array[$i];
if ($a->teacher_id==$teacher_id) {
break;
}
}
Either the sizeof function has had a serious decrease in its speed or something is awry..
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 13:00:01 2025 UTC |
Please reopen this issue. It still exists in version 4.3.0. I'm running on OSX. The performance with a large array that uses sizeof() in a loop is very poor. It's abut 50X slower than it should be. The following takes 8-10 seconds: for ($i=1;$i<=sizeof($spell);$i++) { $sentence=str_replace($spell[$i][0],$spell[$i][1],$sentence); } Whereas the following takes only .18 seconds: $end=sizeof($spell); for ($i=1;$i<=$end;$i++) { $sentence=str_replace($spell[$i][0],$spell[$i][1],$sentence); }I see about a 20% overhead in executing the two loops with a no-op inside the loop. This amount of overhead is expected (due to the fact that your are making an extra function call on every iteration). Nowhere near 5000%. Tested on OSX with this: <?php for($i=0;$i < 100000; $i++) { $array[] = '1'; } for($i=0; $i < sizeof($array); $i++) { } ?> vs. <?php for($i=0;$i < 100000; $i++) { $array[] = '1'; } $end = sizeof($array); for($i=0; $i < $end; $i++) { } ?> Your code fragment is not complete, so I cant actually repliate your test.Here's more of the code, previous to the loops: $sentence="I acn run to the store adn sing without knowing why." $num=0; $num++; $spell[$num]=Array(" acn "," can "); $num++; $spell[$num]=Array(" adn "," and "); // there are about 800 of these types of corrections. I expect you dont want me to include them all. My tests today show a difference of .18 seconds to 3.4 seconds. That's about 20X slower. Better for some reason, but still not optimal by a long shot.