Reply to Re: string concat vs. join/implode

Your name:

Reply:


Posted by Sarel on 05/15/07 07:34

On May 15, 12:02 am, ZeldorBlat <zeldorb...@gmail.com> wrote:
> 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().

I think with the modern Servers that companies use, with usually
enough RAM to go around one shouldn't be too concerned with what the
Garbage Collector does. In my experience, the most important thing to
worry about is speed, as ZeldorBlat demonstrated here. Remember, your
script will be ran on the server for a fraction of a second if it's a
normal web page, and then will most probably not be ran for some time
until the next user hits that page. If you see that this script has a
problem in terms of memory usage, then yes - try and figure out how to
use less memory and be more effective. But if not, rather worry about
speed. Do test like Z did with the profiler and use the fastest.
That's usually also a good indicator of what's best for the GC as
well, 'cause if something runs faster, it generally means that the GC
didn't have to do so much work which would off course take some time
to do. My rule of thumb is:
1) If you run out of memory in your script, start unsetting variables
once you're done with it. If that works, cool!
2) If (1) didn't work, then start benchmarking and see what's taking a
crapload of time, concentrate on making that work faster - usually you
sort out your memory leak / problem out right there and then.
3) If (2) didn't work, admit you're in trouble and look at the script
as a whole - see if you couldn't have done it some other way - think
outside the box on this one then ;-)

[Back to original 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

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