News for hackers. Stuff that matters.
Re:On Stack Overflow's recent battles with the .NET Garbage ... by stonemetal on Friday October 28, 2011 @08:37AM (#3167980)
In C# strings are immutable , so string + string creates a new string. This means coping both strings into the new result string. That is a lot of copying and can be pretty slow if you do it on large strings, or often on small strings. Especially if you concatenate multiple strings at once (A + B + C + D means that A is copied 3 times, B 3 times, C twice, and D once) StringBuilder is mutable so appends don't recopy data. It is really only a problem if you do it on large strings more than a few times.

On the other hand I am not sure if this is a problem in .NET. I know the semantics look bad but no one said the execution model had to match the semantics. If I remember correctly the JVM has the capability to rewrite the code to the faster version, though I am not sure if the CLR does.

Re:On Stack Overflow's recent battles with the .NET Garbage ... by m_myers on Friday October 28, 2011 @12:37PM (#3168962)
The JVM does rewrite individual concatenations as StringBuilders (so a+b+c+d results in one StringBuilder with three .append() operations), but the real performance problem is concatenating in a loop.

    String result = "";
    for (String str : myStrings) {
        result += str + " "; // on each execution, a new StringBuilder is created
    }
as opposed to

    StringBuilder result = new StringBuilder();
    for (String str : myStrings) {
        result.append(str).append(" ");
    }
The JVM can't hoist the generated StringBuilder out of loops to produce the second, faster piece of code. I believe the CLR is the same.
Re:On Stack Overflow's recent battles with the .NET Garbage ... by njw45 on Friday October 28, 2011 @02:06PM (#3169359)
Later JVMs do escape analysis, though - so there's not necessarily any difference between the two methods...
Re:On Stack Overflow's recent battles with the .NET Garbage ... by m_myers on Friday October 28, 2011 @05:51PM (#3170249)
At least in Oracle Java 1.6 update 24, which is supposed to have escape analysis enabled by default, it is a huge difference. I don't know if Java 7's analysis is smarter.