|
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
Navigation:
[Reply to this message]
|