"Pascal H" <mm*@mmm.com> wrote in
news:40*********************@news.free.fr:
I discovered something which surprised me today:
version 1/
$toto = "";
while (mysql->fetchrow) (about 400 times)
{
$toto .= "row1 row2 row3";
}
=> took 60s
version 2/
array $toto;
while (mysql->fetchrow) (about 400 times)
{
$toto[] = "row1 row2 row3";
}
is veryyyy faster ... 3s !
I discovered a difference with perl too but version 1 takes 8s only
String concatenation will generally be slower than adding an element to an
array because it's usually implemented by allocating a new block of memory
big enough to hold both strings, copying the original string over to it,
and then copying the new string to it. Adding an element to an array is
mostly just a matter of allocating memory for the new string and copying
it; the original contents of the array don't have to be copied.
This description is obviously oversimplified, but the main point is that
when you're concatenating, the original string has to be copied, and the
time this takes increases with each successive concatenation since the
original string is getting longer.
To get a little mathematical and use an oversimplified example (namely
dealing with N strings, all of the same length M characters), adding them
to an array will require N*M characters to be copied, so the run time is
proportional to N. We say that it "runs in linear time."
With concatenation, the first string has to be copied N times, the second
string N-1 times, and so on until the last string has to be copied only
once. This means that (N+(N-1)+(N-2)+...+1)*M characters have to be
copied, which works out to (N**2+N)*M (using "N**2" to mean "N squared"),
so the run time is now proportional to the square of N. We say that it
"runs in quadratic time."
The result is that if you double the number of strings, the array-insertion
method will take twice as long, but the concatenation method will take 6
times as long.
Of course an interpreter can optimize things a bit, for example by
allocating extra memory for each string so that in some cases that
concatenated string can just be appended onto the end of the original, but
there's a limit to how much this can be done. The main problem is that in
realistic scenarios, the interpreter has no idea how big the next string to
concatenate is going to be.