Chris wrote:
> Jonas Rosling wrote:
> 
> ...
> 
>> while ($count < count($salespersons)) {
> 
> 
> I see you have solved it - just one comment.
> 
> This particular line will get evaluated every time. If you have a large
> number of elements in $salespersons, it will slow things down considerably.

Quick amendment to this statement: PHP stores the number of elements in
a userspace array directly in the internal zval, so calling count()
simply reads that value no matter how big the array is (in algorithmic
terms, it is O(1)).  In other words, count(array(1)) is every bit as
efficient as count(array_pad(array(1), 1000000, 1)) if you eliminate the
cost of creating the 1000000 element array.

Having said this, there is potentially significant overhead involved in
calling a function as opposed to a variable lookup, and so you might end
up saving a few microseconds.  However, because count() is O(1), it will
be a constant amount per loop.  If you need to eke out that much extra
performance, you may be better off using a profiler to see where the
real bottlenecks in the code are.

Greg

> 
> From a performance point of view you're much better off doing:
> 
> $sales_count = count($salespersons);
> 
> while($count < $sales_count) {
> ....
> 
> The only reason to leave the count in the while statement is if you are
> adding/removing to that array within the loop, otherwise chuck it in a
> temp variable and use that instead.
> 

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

Reply via email to