note 112989 added to function.count
| From: | semmelbroesel at gmail dot com | Date: | Thu, 15 Aug 2013 14:14:31 +0000 |
| Subject: | note 112989 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195976@lists.php.net to get a copy of this message | ||
I've read this somewhere else before, but experienced it first hand now, so I thought I'd
post it here.
While in regular use, count() is pretty fast, this changes rapidly when used in a for loop. Example:
for ($x = 0; $x < count($array); $x++)
{
// some code here
}
If your array contains a lot of data, this code gets slowed down significantly. Better way to write
this:
$arrayCount = count($array);
for ($x = 0; $x < $arrayCount; $x++)
{
// some code here
}
I didn't think it would make such a difference, but it did in my case - changed 15 seconds load
time into 1 second!
----
Server IP: 69.147.83.199
Probable Submitter: 75.146.182.131
----
Manual Page -- http://php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/112989
Del: integrated -- https://master.php.net/note/delete/112989/integrated
Del: useless -- https://master.php.net/note/delete/112989/useless
Del: bad code -- https://master.php.net/note/delete/112989/bad+code
Del: spam -- https://master.php.net/note/delete/112989/spam
Del: non-english -- https://master.php.net/note/delete/112989/non-english
Del: in docs -- https://master.php.net/note/delete/112989/in+docs
Del: other reasons-- https://master.php.net/note/delete/112989
Reject -- https://master.php.net/note/reject/112989
Search -- https://master.php.net/manage/user-notes.php