Re: array count

From: Date: Sun, 03 Oct 1999 12:57:28 +0000
Subject: Re: array count
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-11467@lists.php.net to get a copy of this message
On Sun, 3 Oct 1999, KIM wrote: > Hi all, > > Following code, > > $num = count($array); > for ($i = 0; $i < $num; $i++) { > ... > } > > runs significantly fater than following code, if $array contains > more than 5000 elements. > > for ($i = 0; $i < count($array); $i++) { > ... > } > > I thought that count() must walk through the array to > number how many elements it has. But the PHP4(zend) doesn't > do that walk, it just returns the number of elements directly. > > zend_hash.c: return ht->nNumOfElements; > > Am I wrong? yes, you are - php3 does not count the elements as well - the secret is that count() in php3 copies the paramters before it gets passed to the function in php4 we don't do that anymore. tc > > Is it interpreting overhead, than makes the difference between > the two codes above? > > Note that following code runs faster enough, > > for ($i = 0; $i < $num; $i++) { > $dummy = count(array()); // dummay, do nothing but > // call meaningless count() > ... > } > > In this code, the interpreting overhead is same as the second > code above. > > --KIM Hiongun. > Thies C. Arntzen "One Big-Mac, Small Fries and a Coke!" Digital Collections Phone +49 40 235350 Fax +49 40 23535180 Hammerbrookstr. 93 20097 Hamburg / Germany

« previous php.dev (#11467) next »