posted 15 years ago
Disagree. Memory is cheap, but on modern applications, processor power is expensive. If you start creating multiple String objects, each has to have a memory location found, and then deleted by the garbage collector. You gain a considerable performance advantage by using a StringBuilder, particularly if you can predict the size of the final String and therefore allocate enough memory at the start of the method.
To go from an Intel Core 3.06GHz to a 3.33GHz would cost me £321 extra (US$530, €360, 25500 Rupees) (a Pentium would cost much less) whereas an extra 1GB of memory would cost £30 (2400 Rs, €33, $50) and an extra 4GB would cost barely twice that.
You avoid frequently reallocating memory with a StringBuilder, so it gives much faster performance, even if you have lots of free heap space all the time.