Reply to Re: For loop and while loop. which one is faster?

Your name:

Reply:


Posted by Thomas Mlynarczyk on 03/29/07 16:30

Also sprach mgcclx@gmail.com:

> For loop and while loop. which one is faster?

I don't think it makes much of a difference internally.

for ( $i = 0; $i < 10; $i++ )
{
// do stuff
}

is probably treated just like the - less elegant -

$i = 0;
while ( $i < 10 )
{
// do stuff
$i++;
}

with no performance gain or loss. On the other hand, it makes a huge
difference in performance if you write

for ( $i = 0; $i < count( $a ); $i++ ) { /* do stuff */ }

or

for ( $i = count( $a ); $i--; ) { /* do stuff */ }

the latter being much faster (function calls - count() in this example -
cost time and the second version is one statement shorter). Of course, it
iterates backwards over $a, but there are many cases where this does not
matter. So, if you want to optimize your code, reduce the number of function
calls, reduce the number of statements and reduce the number of variables
(especially for intermediate results).

Greetings,
Thomas

[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

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