"When concatenation strings in Java, you should use StringBuilder rather than using the + operator on String" "Performance changes are not so when there are few string concatenations, but it is better to use StringBuilder when there are many." I sometimes hear that.
StringBuilder is more efficient than anything else when you actually perform a large number of string concatenations repeatedly, but over there and [here](https:: //qiita.com/ota-meshi/items/40d5dcb0e99574a8a368#stringbuilder%E3%81%A8stringbuffer) has already obtained the verification result, so I will leave it.
However, when actually coding, for example, when you want to enclose a character string with [character string] and display it, there are many cases where the character string concatenation is performed only a few times. In that case, it is useless to create a new instance of StringBuilder, put a character string in it, concatenate the character string only a few times, and then return it to the String type. ..
So how many times should I change to StringBuilder after string concatenation? Even if I googled it, it didn't come out easily, so I checked it myself.
The measurement method is
When using the + operator: Declare a character string → combine appropriate character strings n times
PlusTest.java
public static void main(String[] args) {
for(int j = 0; j < LOOP; j++){
String str = "s";
plus(str, BUILD_NUM);
}
}
private static String plus(String str, int n) {
for (int i = 0; i < n; i++) {
str += "p";
}
return str;
}
When using StringBuilder: Declare a string → Create a StringBuilder instance → Combine appropriate strings n times
StringBuilderTest.java
public static void main(String[] args) {
for(int j = 0; j < LOOP; j++){
String str = "s";
sb(str, BUILD_NUM);
}
}
private static String sb(String str, int n) {
StringBuilder sb = new StringBuilder().append(str);
for (int i = 0; i < n; i++) {
sb.append("b");
}
return sb.toString();
}
This series of steps is repeated 10 million times, and the time taken is measured. By the way, the measurement is repeated about 10 times to get the median value.
0 joins
Once
Twice
3 times
The result was disappointing. By the way, the measurement time of StringBuilder varied considerably, and sometimes 1 time was shorter than 0 times of join (just declared). If the number of couplings increases a few times, it seems that it will fluctuate only within the margin of error. Either way, it's usually faster than using the + operator.
When I try to concatenate strings with the + operator, it seems that the compiler creates a StringBuilder instance and concatenates it (Reference). So, "when using the + operator: declare a string-> combine appropriate strings n times" is correctly "declare a string-> (create a StringBuilder instance-> combine) * n". Will be. However, even if it is combined only once, it takes twice as long as the measurement result. Are you creating a string instance in vain?
If you only want to combine once, the compiler will do it for you, so it should be either (rather, it is better to use the + operator because the code is messy), but if you are nervous, it is better to declare StringBuilder and use it. I feel like. If you're spinning more than once in a loop, it's better to use StringBuilder to reduce unnecessary StringBuilder declarations.
[^ 1]: It's an unreliable number because I only got either 0ms or 16ms, but I'm ignoring it because I'm just measuring the time of the code that just wastes the for statement in the first place.