|
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]
|