|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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";
}
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 22:00:01 2025 UTC |
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..