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

From: Date: Sat, 01 Mar 2003 18:51:00 +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-34586@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: 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); } Previous Comments: ------------------------------------------------------------------------ [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 (#34586) next »