To give you an idea of how slow that is, maximum memory throughput with memcpy on many modern systems is measured in the tens of gigabytes per second. The comparison to .NET at the end is amusing, because the same benchmark including the random generation of characters can be done many times faster in C#. And I'm not even good at writing C#, having spent most of my time in Haskell. But I did it anyway.
PHP version (in C#) = 1.8660s
Using prebuilt array = 0.0654s
Using StringBuilder, random = 1.5628s
Using StringBuilder, array = 0.0749s
EDIT: If I construct the StringBuilder with a size parameter, it shaves another 0.02 seconds off (or 25% of execution time).Gist for source: https://gist.github.com/AaronFriel/9699764
The original should be rewritten to not be a test of PRNG throughput. In the amount of time that it took PHP to generate those random strings, my unoptimized, first-try C# concatenates about a hundred times faster.
[1]I am aware that this is merely the size of the resultant string - 20 characters times 1,000,000 iterations. But it's within a factor of two of the total bytes copied, and I don't know PHP's internal string representation. I don't know if each append alters an entire string, rewrites a rope structure, etc. In all likelihood, many fewer than 20 million bytes were harmed by PHP in the making of this benchmark.
Edit: Does PHP even have any per-call profilers?
Would it be relevant to test this using file reads?
Not sure about the file reads, defeating string interning seemed to be the point that helped show any really difference. Quite an interesting/cool topic that.