| 
	
 | 
 Posted by "Steve McGill" on 12/06/05 18:04 
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@grant.org.uk> schreef in bericht  
news:4395B271.4030303@grant.org.uk... 
> 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/
 
  
Navigation:
[Reply to this message] 
 |