Re: Unnecessary if statement? Programming technique

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

 



Hi,

I imagine this kind of thing is not especially taxing on the processor,
especially if the condition is a fairly simple comparison.  That said, I
have very little understanding aside from my own limited experience of
what runs slowly!

If you're worried about code maintenance, then move the code out to a
function and pass the parts that vary as parameters.

Cheers,

David Grant

Steve McGill wrote:
> Hi,
> David is right about the unwanted side-effect. Thanks for the idea though.
> Unfortunately the 'greater problem' is not so great, I've just been doing 
> this for a while now and find myself programming loops like these so often 
> and I've never got round to testing if a simple IF statement is a major 
> drain on the CPU. Somehow I doubt it.
> I got this reply from someone direct to my mail address, which seems to sum 
> it up:
> 
> --
> In truth you are not evaluating the whole if block just the condition
> and since its such a simple condition I can't see how it would be at
> all taxing on the server. In your specific case I can't think of a
> better way to do it either.
> --
> 
> I'll try and think of a better example:
> 
> <?
> $bool = true; // this is set dynamically and not known in advance
> while(true) {
>   if($bool) { // this condition tested in every single loop
>     // do first code
>   } else {
>     // do second code
>   }
> }
> ?>
> 
> and I am wondering if the compiler is smart enough to turn this into:
> 
> <?
> $bool = true; // this is set dynamically and not known in advance
> if($bool) { // this condition only tested once
>   while(true) {
>     // do first code
>   }
> } else {
>   while(true) {
>     // do second code
>   }
> }
> ?>
> 
> I realise this might be hard to follow without giving specific examples and 
> code.
> 
> In this case, the coding style of the 2nd example seems far better, but 
> sometimes the 2 blocks of code are practically identical and it's a 
> programmer's nightmare to have the blocks of code in 2 places and to 
> remember to keep them both updated.
> 
> I'm also assuming that using function calls is also much slower than 
> evaluating a very simple IF statement.
> 
> Thanks for your interest.
> 
> Best wishes,
> Steve
> 
> "David Grant" <david@xxxxxxxxxxxx> schreef in bericht 
> news:4395B271.4030303@xxxxxxxxxxxxxxx
>> Jared Williams wrote:
>>> Why not
>>>
>>> for ($i = 0; $i < 1000000/100; ++$i)
>> This involves dividing 1000000 by 100 for each iteration of the loop.
>> It would be better to test against 10000.
>>
>> There is also the unwanted side-effect of executing the code on each
>> hundredth iteration, which is unwanted (as far as I understand the
>> problem). :)
>>
>> It would be interesting if Steve could divulge the greater problem that
>> he is seeking a solution to.
>>
>> Cheers,
>>
>> David
>> -- 
>> David Grant
>> http://www.grant.org.uk/ 
> 


-- 
David Grant
http://www.grant.org.uk/

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