Re: Sanity checker?
| From: | David Tulloh | Date: | Sat, 08 Jul 2006 13:09:50 +0000 |
| Subject: | Re: Sanity checker? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-239214@lists.php.net to get a copy of this message | ||
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.
David