Re: Assignment (Was Re: [PHP] More microptimisation (Was Re: [PHP] Variable as an index)

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

 



On Tue, 23 Dec 2008 10:25:13 -0500, tedd.sperling@xxxxxxxxx (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

-- 
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