Reply to Re: [PHP] Unnecessary if statement? Programming technique

Your name:

Reply:


Posted by tg-php on 12/06/05 18:49

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, 999999, 'echo \"test standard\";');
$arr[100] = 'echo \"test special\";';

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

or....

<?php
$arr = array_fill(0, 999999, 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;$i<1000000;$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 999999
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;$i<100;$i++)
// do something standard


// do something special for $i = 100

for($i=101;$i<1000000;$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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация