When concatenating strings in Java, there are two ways to connect them, either by connecting them with + =
in String type or by connecting them with ʻappend ()` of StringBuilder.
The results are the same for both, but there is a difference in processing speed.
For example, this code
String stringResult = null;
//String concatenation with a String object
for (int i = 0; i < 10; i++) {
String str = Integer.toString(i);
stringResult += str;
}
Since the String type is a String object, it means that a String type variable is declared = a String object is created.
Therefore, the String object is created at the timing of stringResult + = str;
which is concatenation of strings.
In other words, String objects are created for the number of loops in the for statement.
For example, this code
StringBuilder sb = new StringBuilder();
//String concatenation with StringBuilder
for (int j = 0; j < 10; j++) {
sb.append(Integer.toString(j));
}
The result is the same as the String example.
However, ʻappend ()` does not create a String object. Just add it to the end.
Processing is faster because the String object is not created.
I measured the difference in processing time between String and StringBuilder with the following program.
public class StringBuilderSample{
public static void main(String[] args) {
final int MAX_COUNT = 100;
System.out.println("Number of joins:" + MAX_COUNT + "When");
/*********************For String type*********************/
String stringResult = null;
//Time taken to process with String
long sTime;
//Start time
long startTime = System.nanoTime();
//String concatenation by String type
for (int i = 0; i < MAX_COUNT; i++) {
String str = Integer.toString(i);
stringResult += str;
}
//ending time
long endTime = System.nanoTime();
//Calculate the time taken for processing
sTime = endTime - startTime;
System.out.println("Processing time in String:" + sTime + "Nanoseconds");
/*********************For StringBuilder*********************/
StringBuilder sb = new StringBuilder();
//Time taken by StringBuilder
long sbTime;
//Start time
startTime = System.nanoTime();
//String concatenation with StringBuilder
for (int j = 0; j < MAX_COUNT; j++) {
sb.append(Integer.toString(j));
}
//ending time
endTime = System.nanoTime();
//Calculate the time taken for processing
sbTime = endTime - startTime;
System.out.println("Processing time in StringBuilder:" + sbTime + "Nanoseconds");
System.out.println("StringBuilder is better" + (sTime - sbTime) + "Nanoseconds fast");
}
}
It is a program that displays the processing speed when string concatenation is performed as many times as MAX_COUNT
.
Below, we will look at the difference in processing time while increasing the value of MAX_COUNT
to 100, 1000, 10,000, 100,000.
By the way, nanoseconds are 10 ^ (-9) seconds (= 0.000000001 seconds).
About 0.57 seconds faster
About 27 seconds faster
It doesn't bother me so much because the difference is up to about 1000 times in microseconds (10 ^ (-6)) at most.
From around 10,000 times, the difference in processing time becomes anxious.
Well, it's true that StringBuilder is faster no matter how many times you run it, so unless you have a specific reason, you should do string concatenation with StringBuilder.
Actually, this article is a reprint from the blog. I also post about other things on my blog.
2018 07/21 Addendum: I wrote a sequel → [java] Do not use "+" in append!
Recommended Posts