|
Posted by frugalprogrammer on 05/14/07 19:05
On May 14, 11:27 am, NoWhereMan <nowheremanNOS...@flashmailSPAM.com>
wrote:
> Maybe a stupid question.
>
> -------------------------
> 1
> -------------------------
> $str = '';
>
> for($i=0; $i<10; $i++)
> $str .= $i;
>
> echo $str;
>
> -------------------------
> 2
> -------------------------
> $str = array();
>
> for($i=0; $i<10; $i++)
> $str[]= $i;
>
> echo implode('', $str);
>
> -------------------------
>
> I suppose the latter will be more performant because there's less work for
> the garbage collector, am I right?
>
> thanks
>
> --
> NoWhereMan (e.v.)
> --http://www.nowhereland.it
> --http://flatpress.nowhereland.it
I would say probably not, as (1) is touching each element n times,
whereas (2) is most likely n*ln(n) in the best case. For i=10, it'll
double. implode() probably just runs your (1) again, appending each
element. I'm not entirely sure how the GC operates in PHP or what kind
of performance hit you might take, but it seems like copy-on-write
could make dereference in (1) not happen as often as you may think.
Regards,
Andrew
[Back to original message]
|