Re: Sanity checker?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, 2006-07-08 at 09:09, David Tulloh wrote:
> Ezra Nugroho wrote:
> > It was a long time since I post any question to this list, like back at
> > php 3x time. Boy, a lot has change now.
> > 
> > So I actually did kick some funny bones, not quite flames yet. And
> > that's good, I don't really like that.
> > 
> > <Wolf>
> > We aren't going to take the time
> > to answer a rhetorical question when you can STFW, RTFM, or RTA.
> > </Wolf>
> > 
> > Who are "we"? I hope you are not talking about php community in general.
> > I would be really sad if that's true. Unfortunately, it seems like
> > that's the trend in this list. I want newbies to succeed, hence my talk
> > about such tool. O.W. newbies will go to RoR instead.
> > 
> > Anyways,
> > 
> > Have you ever seen things like
> > 
> > for ($i = 0; $i < count($some_array); $i++) {
> >   //do stuff
> > }
> > 
> > 
> > Do you know how slow it is if $some_array gets big compared to 
> > 
> > $array_count = count($some_array);
> > for ($i = 0; $i < $array_count; $i++) {
> >   //do stuff
> > }
> > 
> > 
> > Of course you do!
> > But newbies might not....
> 
> Of course!  Every time you ask for the count of an array PHP loops
> through every item meaning that a doing many counts on a large array (an
> O(n^2) operation) reduces your program to a crippling crawl.
> 
> Hang on a tic, that doesn't sound like the PHP that I know.
> 
> PHP knows the size of the array, doing a count(array) just returns an
> existing internal number.  The count() function doesn't get any slower
> with the array size and calling a simple function isn't significantly
> slower than accessing a variable.  In fact, the above examples with an
> array of 100,000 elements didn't result in either script being
> consistantly faster than the other.


Ahh but, precomputing the size and assigning to a variable and using the
variable instead WILL provide a speed gain since you will save the
function call overhead for X - 1 interations. Operator performance in
PHP is MUCH faster than function performance. And with that said, it's a
good idea anyways to get into the habit of using a precomputed value
when iterating since unless you specifically know the internal design of
every function, you may just be hitting an O( n lg n ) algorithm or
worse.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux