Re: Assignment (Was Re: [PHP] More microptimisation (Was Re: [PHP] Variable as an index)
| From: | Clancy | Date: | Wed, 24 Dec 2008 00:06:21 +0000 |
| Subject: | Re: Assignment (Was Re: [PHP] More microptimisation (Was Re: [PHP] Variable as an index) | ||
| References: | 1 2 3 4 5 6 7 8 9 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-285025@lists.php.net to get a copy of this message | ||
On Tue, 23 Dec 2008 10:25:13 -0500, tedd.sperling@gmail.com (tedd) wrote:
>At 9:10 AM +1100 12/23/08, Clancy wrote:
>>Schlossnagle (in "Advanced PHP Programming") advises:
>>
>>$i = 0; while ($i < $j)
>> {
>> ........
>> ++$i;
>> }
>>
>>rather than:
>>
>>$i = 0; while ($i < $j)
>> {
>> .......
>> $i++;
>> }
>>
>>as the former apparently uses less memory references. However I
>>find it very hard to
>
>Two things:
>
>1. One statement, one line.
>2. The code between the two examples is different; produces different
>results; and thus is rather pointless in making a definitive
>comparison.
>
>Assignment, demonstrate a correct way to test ++i vs i++.
>
>Cheers,
>
>tedd
Spoken like a true demagogue -- nitpicking about trivial points of style, but displaying
total ignorance of elementary rules of programming.
One of the things I like about Schlossnagle's book is that he gives commonsense advice.
Specifically he advises against obsessing about other people's style rules, but suggests
that you choose a style that you like, and use it consistently.
And as the examples are written they do exactly the same thing; they each go through the
empty loop $j times. I had rashly assumed that anyone reading this discussion group would
understand that they were shorthand for something like the following:
$i = 0; while ($i < $j)
{
If ($a[$i])
{
[ some operation involving $i]
}
$i++;
}
$i cannot be incremented at the start as it is used inside the conditional expression, nor
can it be implemented inside the expression or you would get an infinite loop the first
time you missed it, so it has to be incremented at the end of the loop, and it is
immaterial whether you choose ++$i; or $i++;
Clancy