RE: [PHP] Unnecessary if statement? Programming technique

2005-12-06 Thread Jared Williams

Hi,
   Why not

for ($i = 0; $i  100/100; ++$i)
{
for ($j = 0; $j  100; ++$j)
{
   // do something standard
}
// do something special for this occurence  
}


Jared

 -Original Message-
 From: Steve McGill [mailto:[EMAIL PROTECTED] 
 Sent: 06 December 2005 10:18
 To: php-general@lists.php.net
 Subject: [PHP] Unnecessary if statement? Programming technique
 
 Hi everyone
 
 Quick question:
 
 If I have such a loop:
 
 ?
 for($i=0;$i100;$i++) {
   if($i==100) {

   }
   // do something standard
 }
 ?
 
 In this case it seems such a waste that the if() statement is 
 done 99 times when it's not needed. Is there any obvious 
 trick that I am missing? 
 I'm not sure how taxing a simple if() statement is on a 
 server, maybe it's negligible, or is it something to worry about?
 
 Something which I'd prefer NOT to do:
 
 ?
 for($i=0;$i100;$i++) {
   // do something standard
 }
 
 // do something special for $i = 100
 
 for($i=101;$i100;$i++) {
   // do something standard
 }
 ?
 
 as I would have have to either keep two copies of the code or 
 write a function just for this purpose, which hardly seems worth it.
 
 Thanks to anyone who takes the time to think about my 
 question and/or respond.
 
 Best wishes,
 
 Steve McGill 
 
 --
 PHP General Mailing List (http://www.php.net/) To 
 unsubscribe, visit: http://www.php.net/unsub.php
 

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



Re: [PHP] Unnecessary if statement? Programming technique

2005-12-06 Thread David Grant
Jared Williams wrote:
 Why not
 
 for ($i = 0; $i  100/100; ++$i)

This involves dividing 100 by 100 for each iteration of the loop.
It would be better to test against 1.

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/

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



Re: [PHP] Unnecessary if statement? Programming technique

2005-12-06 Thread Steve McGill
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 [EMAIL PROTECTED] schreef in bericht 
news:[EMAIL PROTECTED]
 Jared Williams wrote:
 Why not

 for ($i = 0; $i  100/100; ++$i)

 This involves dividing 100 by 100 for each iteration of the loop.
 It would be better to test against 1.

 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/ 

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



Re: [PHP] Unnecessary if statement? Programming technique

2005-12-06 Thread David Grant
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 [EMAIL PROTECTED] schreef in bericht 
 news:[EMAIL PROTECTED]
 Jared Williams wrote:
 Why not

 for ($i = 0; $i  100/100; ++$i)
 This involves dividing 100 by 100 for each iteration of the loop.
 It would be better to test against 1.

 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



Re: [PHP] Unnecessary if statement? Programming technique

2005-12-06 Thread tg-php
This is probably going to sound strange, but I like to try to think outside the 
box (buzzphrase!) and hit things at odd angles.

Would someone care to test (or already know) the performance difference between 
a for loop and a foreach loop?

Or the performance difference over many iterations of if (something == 
somethingelse) versus if (true)?

Two examples to test:

?php
  $arr = array_fill(0, 99, 'echo \test standard\;');
  $arr[100] = 'echo \test special\;';

  foreach ($arr as $value) {
eval($value);
  }
?

or

?php
  $arr = array_fill(0, 99, true);
  $arr[100] = false;

  foreach ($arr as $value) {
if ($value) {
   // do standard
} else {
   // do special
}
  }
?


I'd be curious to see the benchmarks.  I wonder if a if() versus doing the 
value check in a for statement are different speed-wise..   I'm wondering how 
much eval() slows it down.. and I'm wondering if if (value == value2) is 
slower than if (true).

Probably other offbeat ways of doing something like this too.

-TG



= = = Original message = = =

Hi everyone

Quick question:

If I have such a loop:

?
for($i=0;$i100;$i++) 
  if($i==100) 
// do something special for this occurence
  
  // do something standard

?

In this case it seems such a waste that the if() statement is done 99 
times when it's not needed. Is there any obvious trick that I am missing? 
I'm not sure how taxing a simple if() statement is on a server, maybe it's 
negligible, or is it something to worry about?

Something which I'd prefer NOT to do:

?
for($i=0;$i100;$i++) 
  // do something standard


// do something special for $i = 100

for($i=101;$i100;$i++) 
  // do something standard

?

as I would have have to either keep two copies of the code or write a 
function just for this purpose, which hardly seems worth it.

Thanks to anyone who takes the time to think about my question and/or 
respond.

Best wishes,

Steve McGill 


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

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