2

I was wondering the execution speed changes if a programmer concatenates inside a Stringbuilder append() statement, or just uses two append statements instead of one.

I am asking this question to help me figure out why we use the StringBuilder class at all when we can just concatenate.

Concatenation Example:

public class MCVE {

    public static void main(String[] args) {
        String[] myArray = {"Some", "stuff", "to", "append", "using", "the",
                "StringBuilder", "class's", "append()", "method"};

        StringBuilder stringBuild = new StringBuilder();

        for(String s: myArray) {
            stringBuild.append(s + " ");
        }

    }
}

Double-Append() Example:

public class MCVE {

    public static void main(String[] args) {
        String[] myArray = {"Some", "stuff", "to", "append", "using", "the",
                "StringBuilder", "class's", "append()", "method"};

        StringBuilder stringBuild = new StringBuilder();

        for(String s: myArray) {
            stringBuild.append(s);
            stringBuild.append(" ");
        }

    }
}
LuminousNutria
  • 1,883
  • 2
  • 18
  • 44

1 Answers1

3

In theory, yes, the concatenation version will take longer, because under the covers it creates a whole new StringBuilder, appends s, appends " ", and then uses toString to create(!) a string for that to pass to the append you coded. (That's what the compiler does. To know about your specific situation, you'd need to test a benchmark representative of your actual code. After all, the JIT will get involved if it's a hotspot at runtime.)

Of course, you probably won't notice. But still, if you're already using StringBuilder, use it (by doing append twice instead). :-)


(The first paragraph above wouldn't be true if they were both string literals, e.g. "foo" + "bar". The compiler does that concatenation.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thank you! So I guess we just use StringBuilder to avoid creating StringBuilder objects over and over again when we concatenate Strings more than once? – LuminousNutria Jan 01 '19 at 18:22
  • 1
    @LuminousNutria - Yes, in situations where performance (speed and memory consumption) is more important than code clarity. Which is rarely, but sometimes. But if this comes up a lot, you might see if there's some kind of built-and-tested templating engine or library you could use instead. – T.J. Crowder Jan 01 '19 at 18:23
  • 1
    it seems likely that the two stringbuilder operations would be quicker than the concatenation but why not test it? Use System.nanoTime() to see how long it takes to run your for loop a large number of times. – stegzzz Jan 01 '19 at 21:31
  • 1
    @stegzzz - Or [a thorough benchmark](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java). :-) After all, the above is correct in terms of the bytecode created by the compiler, but the JIT will get involved if it's a hotspot in the code. – T.J. Crowder Jan 02 '19 at 08:26