You are here: Re: [PHP] explode a string « PHP « IT news, forums, messages
Re: [PHP] explode a string

Posted by Jochem Maas on 04/20/05 14:17

Richard Lynch wrote:
> On Tue, April 19, 2005 7:03 am, Jochem Maas said:
>
>>The 'other' guy mentioned that while() is faster than foreach,
>>is this true?
>
>
> Don't know ; Don't care.
>
> You should never loop through so many things in PHP that it matters in the
> first place :-)
>
>
>>I read a few days ago somewhere on php.net that foreach() is the
>>recommended (by php devs) way of iterating over arrays....
>
>
> [shrug]
>
> That's probably because they're tired of people not understanding the
> internal pointer array, and asking FAQs about it. Or maybe not. Ask them
> why they prefer it. I sure don't know.
>
>
>>also, compare these 2 lines:
>>
>>while (list(, $idcat) = each($idcats)){ /* ... */ }
>>foreach ($idcats as $idcat){ /* ... */ }
>>
>>now its seems to me that the foreach version is 'up' 2 function calls
>
>
> None of those are function calls.
>
> They are all language constructs. Okay, each() *might* be a function...
>
> I'm not sure how much difference there is in the number of language
> constructs used, nor if they are even comparable in sheer numbers the way
> functions are.

ah yes, lang constructs rather than function calls.

>
> foreach is probably slower, I think, because it creates a *copy* of the
> array to work on, so it won't mess up the original and its internal
> pointer.

unless I'm mistaken its a copy-on-change, so unless you are changing the
the array inside the loop you don't suffer the actuall copy penalty - can anyone
knowledgable on php internals confirm or deny this?

actually now I think of it you can use references in a foreach statement:

php -r '
$arr = array(1,2,3);
foreach($arr as $k => &$v) {
$v++;
}
var_dump($arr);
'

which suggests that a copy is not (always?) being made...

>
> Again, with 200 bytes, you are wasting your time to worry about any of this.

true, It's purely a theoretical interest - deeper understanding is alway nice :-)
....its not even my 200 bytes we're talking about ;-)

>
>
>>on the while loop, all else being equal the foreach loop has to be faster
>>(given that calling functions is relatively very expensive)...
>>or is foreach() _really_ heavy when compared to while()?
>
>
> Why don't you just benchmark it on your own machine and find out?

because I don't have the skills to write a test/benchmark that I _know_ is
kosher (and not skewed by a million of my misconceptions, besides I run so much
stuff on my machine that speed can be severely affected by things like
apache or firebird running in the background....

that and I lazy ;-) (or I just don't care enough to invest time investigating this)

>
>
>>not that I care too much, I find foreach() more pleasing to the eye and
>>there is
>>less to type (in the given example).
>
>
> I'm an old dog, and I don't quite understand for sure how this new-fangled
> foreach thingie works. I'd spend more time looking it up and reading
> about it than just typing what I *know* works. [shrug]



>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация