php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22435 count() is slow when used with large arrays
Submitted: 2003-02-26 08:11 UTC Modified: 2003-02-27 08:48 UTC
From: carl at topthetable dot com Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 4.3.1 OS: Redhat 7.2
Private report: No CVE-ID: None
 [2003-02-26 08:11 UTC] carl at topthetable dot com
count() is increasingly slow when used to count large arrays.  This is using 4.3.1 (compiled from source) on a Redhat 7.2 box.  The box has about 80Mb free, so it's not a paging issue.  It works as expected on Mac OS X running 4.3.1-dev.

Looking at PHP's and Zend's source, I would expect the time taken to return the count() of an array to be similar regardless of the size of the array.

Below is a script to demonstrate the problem.  


<?
  $st = microtime();

  echo "Script started:            ".microtime()."\n";

  dosize( 10 );
  dosize( 20 );
  dosize( 50 );
  dosize( 100 );
  dosize( 200 );
  dosize( 500 );
  dosize( 1000 );
  dosize( 5000 );
  dosize( 10000 );

  function dosize( $y ) {

    echo "Doing size $y\n";

    echo "  Starting array building:".microtime()."\n";

    $t = array();
    for ( $i=0; $i<$y; $i++ ) {
      $t[] = array( array( "3", "1" ), "3", array( array ( "5", "1", "8" ) ) );
    }

    echo "  Array built:            ".microtime()."\n";

    count( $t );

    echo "  Count finished:         ".microtime()."\n";
  }
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-02-26 09:24 UTC] sniper@php.net
Try this script which actually shows the time _spend_ for count:

<?php

        function getmicrotime(){ 
                list($usec, $sec) = explode(" ",microtime()); 
                return ((float)$usec + (float)$sec); 
        }
        
        $tot = 0;

        for ($j=0; $j < 10; $j++) {
                $time = dosize($j * 1000);
                echo $time,"\n";
                $tot += $time;
        }
        
        echo "Avg: ", $tot/$j, "\n";

        function dosize( $y )
        {
                $t = array();

                for ( $i=0; $i<$y; $i++ ) {
                        $t[] = array( array( "3", "1" ), "3", array( array ( "5", "1", "8" )) );
                }

                $start = getmicrotime();
                count( $t );
                $end = getmicrotime();

                return $end - $start;
        }
?>

Results for me:

Linux (double Celeron 500Mhz): 

0.00995302200317
0.000295042991638
0.000288963317871
0.000303030014038
0.000295042991638
0.000295042991638
0.000294089317322
0.000292062759399
0.000293016433716
0.000355005264282
Avg: 0.00126643180847

MacOSX, PowerPC (1Ghz or faster, not sure):

0.00011193752288818
9.0956687927246E-05
0.00010311603546143
0.00010299682617188
0.00010395050048828
0.00010097026824951
0.00011003017425537
0.00013697147369385
0.00010299682617188
0.00010597705841064
Avg: 0.00010699033737183

Nothing wrong here..

 [2003-02-26 09:55 UTC] carl at topthetable dot com
Thanks for your reply, and better script.

Here are my results, showing the times on the Redhat and OSX boxes respectively ...

Redhat 7.2 (PIII 700Mhz):
0.000356078147888
3.09110200405
24.5502359867
55.3548690081
136.626052976
... Gave up waiting.  As you can see, this is not correct behaviour.  You can clearly see that the time isn't constant, nor increasingly linearly.  I therefore say there is something wrong.

OSX (iBook, 500Mhz):
0.00040602684021
0.000491976737976
0.000488996505737
0.000484943389893
0.000471949577332
0.000415086746216
0.000434041023254
0.00104403495789
0.000488996505737
0.000432014465332
Avg: 0.00051580667495

This is as per your results.
 [2003-02-26 10:20 UTC] sniper@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php4-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php4-win32-STABLE-latest.zip

I tested using this and got pretty same results on both linux and macosx. Please test it..

 [2003-02-27 03:36 UTC] carl at topthetable dot com
OK, recompiled with the latest STABLE and I got the same results.  Then in a flash of inspiration, I remembered that I have xdebug installed on the Linux box with collect_params on - Sure enough, when I turn this option off, the problem goes away.

So I'll go away and hide ...
 [2003-02-27 08:48 UTC] sniper@php.net
Derick just mentioned that this bug should be fixed in xdebug now. :)

(bogusing as this is not bug in PHP itself)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 20:01:28 2024 UTC