#6608 [Com]: sizeof($array) extremly slow in loop. 90% cpu util

From: Date: Sun, 02 Mar 2003 18:56:39 +0000
Subject: #6608 [Com]: sizeof($array) extremly slow in loop. 90% cpu util
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-34632@lists.php.net to get a copy of this message
ID: 6608 Comment by: benji at daystream dot com Reported By: mledet at spirenet dot com Status: Closed Bug Type: Performance problem Operating System: Linux 2.2.16 PHP Version: 4.0.2 New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2003-03-01 13:07:11] gschlossnagle@php.net 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. ------------------------------------------------------------------------ [2003-03-01 12:50:59] benji at daystream dot com 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); } ------------------------------------------------------------------------ [2001-05-10 05:55:09] sniper@php.net No feedback. I problem exists with 4.0.5, reopen. ------------------------------------------------------------------------ [2001-04-10 10:12:23] sbergmann@php.net Could you please test your script with either the PHP 4.0.4pl1 release, the latest PHP 4.0.5 RC or a current CVS snapshot from http://snaps.php.net/? Thanks. ------------------------------------------------------------------------ [2000-09-07 13:01:24] 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.. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=6608&edit=1

« previous php.bugs (#34632) next »