You are here: Re: string concat vs. join/implode « PHP Programming Language « IT news, forums, messages
Re: string concat vs. join/implode

Posted by Rami Elomaa on 05/14/07 19:14

NoWhereMan kirjoitti:
> 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);
>
> -------------------------

3:

echo implode('', range(0,9));

(I suppose 2 is eactly the way range() is implemented, so there should
be not much difference...)

> I suppose the latter will be more performant because there's less work for
> the garbage collector, am I right?

Why don't you benchmark them for comparison? Run both in a loop for
thousands of times and take time for both and check how much memory they
consume. You'll need memory_get_usage(), maybe memory_get_peak_usage()
and microtime().

If I had to guess, I'd say there is no huge difference between
concatenating a char and adding an element to an array, but what will
tip the scale is that you need to use implode in the second version,
that'll create some overhead for the second version.

--
Rami.Elomaa@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

 

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

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