note 82588 added to function.sizeof
| From: | rrf5000 at psu dot edu | Date: | Thu, 17 Apr 2008 14:54:38 +0000 |
| Subject: | note 82588 added to function.sizeof | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138409@lists.php.net to get a copy of this message | ||
To explain previous comments: the reason that the FOR loop runs faster on LARGE arrays if you set
its size to an array first (using count() or sizeof() ) is pretty simple. It's because if you
use
<?php
for ($i = 0; $i < sizeof($huge_array); $i++)
{
code
}
?>
every time the FOR loop checks its condition (is $i less than sizeof($huge_array) ), it must run the
sizeof() function on $huge_array again. This is what causes the extra processing time, which can
become significant on larger arrays.
As a side note, this has been thoroughly commented on both correctly and incorrectly in the User
Contributed Notes for the count() function. Users Jaik, Wulfson, and Anonymous have summed this
particular matter up pretty well in their posts http://www.php.net/manual/en/function.count.php#78741
, http://www.php.net/manual/en/function.count.php#78931
, and http://www.php.net/manual/en/function.count.php#79238
.
----
Server IP: 69.147.83.197
Probable Submitter: 146.186.149.139 (proxied: 66.71.78.55)
----
Manual Page -- http://www.php.net/manual/en/function.sizeof.php
Edit -- https://master.php.net/note/edit/82588
Del: integrated -- https://master.php.net/note/delete/82588/integrated
Del: useless -- https://master.php.net/note/delete/82588/useless
Del: bad code -- https://master.php.net/note/delete/82588/bad+code
Del: spam -- https://master.php.net/note/delete/82588/spam
Del: non-english -- https://master.php.net/note/delete/82588/non-english
Del: in docs -- https://master.php.net/note/delete/82588/in+docs
Del: other reasons-- https://master.php.net/note/delete/82588
Reject -- https://master.php.net/note/reject/82588
Search -- https://master.php.net/manage/user-notes.php