php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6608 sizeof($array) extremly slow in loop. 90% cpu util
Submitted: 2000-09-07 13:01 UTC Modified: 2003-03-02 13:03 UTC
From: mledet at spirenet dot com Assigned:
Status: Closed Package: Performance problem
PHP Version: 4.0.2 OS: Linux 2.2.16
Private report: No CVE-ID: None
 [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..

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-04-10 10:12 UTC] 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.

 [2001-05-10 05:55 UTC] sniper@php.net
No feedback. I problem exists with 4.0.5, reopen.

 [2003-03-01 12:50 UTC] 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); 
}
 [2003-03-01 13:07 UTC] 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-02 12:56 UTC] benji at daystream dot com
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.
 [2003-03-02 13:03 UTC] gschlossnagle@php.net
What you need to post is a minimal testcase.  If the 
testcase I outlined earlier doesn't behave badly on your 
system (which it does not on mine), You need to come up 
with a small reproduceable _complete_ test case.  Something 
that I can just cut and paste into a script to duplicate 
your results on my system.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 11:01:30 2024 UTC