array count
| From: | KIM | Date: | Sun, 03 Oct 1999 02:14:56 +0000 |
| Subject: | array count | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-11450@lists.php.net to get a copy of this message | ||
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?
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.