Re: Pointers For Newbies, Reminders For Oldies

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2010/1/27 Richard Quadling <rquadling@xxxxxxxxxxxxxx>:
> 2010/1/27 Michael A. Peters <mpeters@xxxxxxx>:
>> Paul M Foster wrote:
>>>
>>> "... should be obvious - but are often overlooked - points within coding
>>> practice that can cause the programmer to develop bad habits and bad
>>> code." - Dan Brown
>>>
>>> Tip #1:
>>>
>>> Don't use count() in loops unless there are very few items to count and
>>> performance doesn't matter, or the number will vary over the loop. That
>>> is, don't do this:
>>>
>>> for ($i = 0; $i < count($items); $i++)
>>>
>>> Instead, do this:
>>>
>>> $number = count($items);
>>> for ($i = 0; $i < $number; $i++)
>>
>> Gah!
>>
>> for ($i=0;$i<sizeof($array);$i++)
>>
>> is something I do all the time.
>> So the array size is being calculated each iteration?
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> for ($i = 0, $j = count($a) ; $i < $j ; ++$i) {
> }
>
> is a very common way to handle that.
>
> Of course...
>
> foreach(range(0, count($a)) as $i) {
> }
>
> is an alternative.
>
> You can see the effect of the counting if you replace ...
>
> count($a)
>
> with ...
>
> mycount($a)
>
> and have ...
>
> function mycount($a) {
>  echo 'Counting : ', count($a), PHP_EOL;
>  return count($a);
> }

<?php
function mycount($a) {
 echo 'Counting : ', count($a), PHP_EOL;
 return count($a);
}

$a = array(1,2,3,4,5,6,7,8,9,10);

echo PHP_EOL;
for($i = 0 ; $i < mycount($a) ; ++$i) {
 echo 'Traditional for() loop ', $i, PHP_EOL;
}

echo PHP_EOL;
for($i = 0, $j = mycount($a) ; $i < $j ; ++$i) {
 echo 'Modern for() loop ', $i, PHP_EOL;
}

echo PHP_EOL;
foreach(range(0, mycount($a)) as $i) {
 echo 'Ultra-modern foreach() with range() loop ', $i, PHP_EOL;
}
?>

outputs ...

Counting : 10
Traditional for() loop 0
Counting : 10
Traditional for() loop 1
Counting : 10
Traditional for() loop 2
Counting : 10
Traditional for() loop 3
Counting : 10
Traditional for() loop 4
Counting : 10
Traditional for() loop 5
Counting : 10
Traditional for() loop 6
Counting : 10
Traditional for() loop 7
Counting : 10
Traditional for() loop 8
Counting : 10
Traditional for() loop 9
Counting : 10

Counting : 10
Modern for() loop 0
Modern for() loop 1
Modern for() loop 2
Modern for() loop 3
Modern for() loop 4
Modern for() loop 5
Modern for() loop 6
Modern for() loop 7
Modern for() loop 8
Modern for() loop 9

Counting : 10
Ultra-modern foreach() with range() loop 0
Ultra-modern foreach() with range() loop 1
Ultra-modern foreach() with range() loop 2
Ultra-modern foreach() with range() loop 3
Ultra-modern foreach() with range() loop 4
Ultra-modern foreach() with range() loop 5
Ultra-modern foreach() with range() loop 6
Ultra-modern foreach() with range() loop 7
Ultra-modern foreach() with range() loop 8
Ultra-modern foreach() with range() loop 9
Ultra-modern foreach() with range() loop 10


So, with the count inline, there are actually 11 calls to the count
compared to 1 in each of the other 2 scenarios.


-- 
-----
Richard Quadling
"Standing on the shoulders of some very clever giants!"
EE : http://www.experts-exchange.com/M_248814.html
EE4Free : http://www.experts-exchange.com/becomeAnExpert.jsp
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731
ZOPA : http://uk.zopa.com/member/RQuadling

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux