[PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Daevid Vincent
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++ ) {}

 -- 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 Optimizer, I don't know if PHP
handles them the same way or not?


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




RE: [PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Ford, Mike [LSS]
 -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++ ) {}
 
  -- 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 Optimizer, I don't know if PHP
 handles them the same way or not?

With bare PHP, yes the first one is quicker.  Don't know if the Zend Optimiser changes 
that.

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 

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




Re: [PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Marek Kilimajer
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 one slower than
the other?

$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 Optimizer, I don't know if PHP
handles them the same way or not?


 



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




Re: [PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Maxim Maletsky
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 elegant programming and you will never have these issues. 


--
Maxim Maletsky
[EMAIL PROTECTED]



Ford, Mike   [LSS] [EMAIL PROTECTED] wrote... :

  -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++ ) {}
  
   -- 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 Optimizer, I don't know if PHP
  handles them the same way or not?
 
 With bare PHP, yes the first one is quicker.  Don't know if the Zend Optimiser 
changes that.
 
 Cheers!
 
 Mike
 
 -
 Mike Ford,  Electronic Information Services Adviser,
 Learning Support Services, Learning  Information Services,
 JG125, James Graham Building, Leeds Metropolitan University,
 Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
 Email: [EMAIL PROTECTED]
 Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211 
 
 -- 
 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] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Kjartan Mannes
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 Optimizer, I don't know if PHP
 handles them the same way or not?

The first one is faster, but it depends on the site of the array and how
often you call the loop. I prefer doing it like this though:

 for($x = 1, $max = count($myArray); $x = $max; $x++ ) {}

For some good optimization (and other) tips check out:
  http://phplens.com/lens/php-book/optimizing-debugging-php.php
  http://www.lerdorf.com/tips.pdf

-- 
Kjartan [EMAIL PROTECTED] (http://natrak.net/)
:: Silence is one great art of conversation.


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




Re: [PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Maxim Maletsky

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 = 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 Optimizer, I don't know if PHP
  handles them the same way or not?
 
 The first one is faster, but it depends on the site of the array and how
 often you call the loop. I prefer doing it like this though:
 
  for($x = 1, $max = count($myArray); $x = $max; $x++ ) {}
 
 For some good optimization (and other) tips check out:
   http://phplens.com/lens/php-book/optimizing-debugging-php.php
   http://www.lerdorf.com/tips.pdf
 
 -- 
 Kjartan [EMAIL PROTECTED] (http://natrak.net/)
 :: Silence is one great art of conversation.
 
 
 -- 
 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] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread .: B i g D o g :.
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...



On Fri, 2002-11-08 at 15:57, Kjartan Mannes wrote:
 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 Optimizer, I don't know if PHP
  handles them the same way or not?
 
 The first one is faster, but it depends on the site of the array and how
 often you call the loop. I prefer doing it like this though:
 
  for($x = 1, $max = count($myArray); $x = $max; $x++ ) {}
 
 For some good optimization (and other) tips check out:
   http://phplens.com/lens/php-book/optimizing-debugging-php.php
   http://www.lerdorf.com/tips.pdf
 
 -- 
 Kjartan [EMAIL PROTECTED] (http://natrak.net/)
 :: Silence is one great art of conversation.
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
-- 
.: B i g D o g :.



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




Re: [PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Marek Kilimajer
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
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...



On Fri, 2002-11-08 at 15:57, Kjartan Mannes wrote:
 

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 Optimizer, I don't know if PHP
handles them the same way or not?
 

The first one is faster, but it depends on the site of the array and how
often you call the loop. I prefer doing it like this though:

for($x = 1, $max = count($myArray); $x = $max; $x++ ) {}

For some good optimization (and other) tips check out:
 http://phplens.com/lens/php-book/optimizing-debugging-php.php
 http://www.lerdorf.com/tips.pdf

--
Kjartan [EMAIL PROTECTED] (http://natrak.net/)
:: Silence is one great art of conversation.


--
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] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread .: B i g D o g :.
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 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
 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...
 
 
 
 On Fri, 2002-11-08 at 15:57, Kjartan Mannes wrote:
   
 
 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 Optimizer, I don't know if PHP
 handles them the same way or not?
   
 
 The first one is faster, but it depends on the site of the array and how
 often you call the loop. I prefer doing it like this though:
 
  for($x = 1, $max = count($myArray); $x = $max; $x++ ) {}
 
 For some good optimization (and other) tips check out:
   http://phplens.com/lens/php-book/optimizing-debugging-php.php
   http://www.lerdorf.com/tips.pdf
 
 -- 
 Kjartan [EMAIL PROTECTED] (http://natrak.net/)
 :: Silence is one great art of conversation.
 
 
 -- 
 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
-- 
.: B i g D o g :.



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




RE: [PHP] For ... = max($myArray) vs For ... = $max

2002-11-08 Thread Daevid Vincent
 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 the 
 top one is 
  the better way to go, but with the Zend Optimizer, I don't 
 know if PHP handles them the same way or not?
 
 The first one is faster, but it depends on the site of the 
 array and how often you call the loop. I prefer doing it like 
 this though:
 
  for($x = 1, $max = count($myArray); $x = $max; $x++ ) {}

Just a little point-out that I used max() not count() because I need to
loop on the *largest value* in the array, not how many *elements* are
actually in the array. But the logic is still sound, and it is true that
calling a function is sub-optimal it seems, even though my array doesn't
change in any way when the for loop is called. PHP still calls the max()
or count() function each time anyways. So alas, I have to create another
variable that serves one single purpose and has a very finite lifespan.
;-)

 For some good optimization (and other) tips check out:
   http://phplens.com/lens/php-book/optimizing-debugging-php.php
   http://www.lerdorf.com/tips.pdf

Thanx for the pointers. I'll check 'em out.

d


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