-Original Message-
From: Daevid Vincent [mailto:daevid;daevid.com]
Sent: 08 November 2002 11:13
Is PHP smart enough to optimize loops?
That is, are these two 'for...loops' equivalent or is one slower than
the other?
$max = max($myArray);
for( $x = 1; $x = $length; $x++ ) {}
As max($myArray) may potentionaly change, it is evalueted everytime (you
may do
$myArray[]=$big_number in your loop). So if you know it is not going to
change, use the first way.
Daevid Vincent wrote:
Is PHP smart enough to optimize loops?
That is, are these two 'for...loops' equivalent or is
The first one is better, and, besides that - it is the most correct way
- there might be something making the array change while inside the loop
- thus you have to do some extremely high calculations to understand the
array does not change runtime, which makes it useless.
Simply do the
Friday, November 8, 2002, 12:13:01 PM, Daevid Vincent wrote:
$max = max($myArray);
for( $x = 1; $x = $length; $x++ ) {}
-- OR --
for( $x = 1; $x = max($myArray); $x++ ) {}
My gut instinct tells me since PHP is interpreted, that the top one is
the better way to go, but with the Zend
the most correct way is probably:
$size = sizeof($myArray);
for( $x = 1; $x = $size; $x++ ) {}
But, this is too personal and is my way.
--
Maxim Maletsky
[EMAIL PROTECTED]
Kjartan Mannes [EMAIL PROTECTED] wrote... :
Friday, November 8, 2002, 12:13:01 PM, Daevid Vincent wrote:
$max =
Then only problem with doing it like
for($x = 1, $max = count($myArray); $x = $max; $x++ )
is that the $max = count( $myArray ) is always verified in each
loop...slowing the for loop down...
faster to do
$max = count( $myArray );
for( $x = 1, $x = $max; $x++ )
just my $0.02...for the day...
If you reed the code carefully, it first assigns $x=1 and
$max=count($myArray),
then it compares $x with $max
.: B i g D o g :. wrote:
Then only problem with doing it like
for($x = 1, $max = count($myArray); $x = $max; $x++ )
is that the $max = count( $myArray ) is always verified in each
Sorry bro,
was thinking that in the initialization section that it would run the
count again, but that is not the case...
$max is initialized first and never looked at again during the for
loop...
On Fri, 2002-11-08 at 19:00, Marek Kilimajer wrote:
If you reed the code carefully, it first
From: Kjartan Mannes [mailto:kjartan;zind.net]
Friday, November 8, 2002, 12:13:01 PM, Daevid Vincent wrote:
$max = max($myArray);
for( $x = 1; $x = $length; $x++ ) {}
-- OR --
for( $x = 1; $x = max($myArray); $x++ ) {}
My gut instinct tells me since PHP is interpreted, that
9 matches
Mail list logo