> 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.


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

Reply via email to