|
Posted by ZeldorBlat on 05/14/07 22:02
On May 14, 3:14 pm, Rami Elomaa <rami.elo...@gmail.com> wrote:
> 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.Elo...@gmail.com
>
> "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
> usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
I replaced 10 with 100000 and ran each function through a profiler.
The first one (using concatenation) was approximately 25% faster than
the one using implode().
[Back to original message]
|