"Please explain the difference between StringBuilder and StringBuffer." Said, "String Builder has better performance !!" I could only answer.
So this time I tried to verify why StringBuilder has good performance and how much the speed difference is.
StringBuilder: Not thread safe StringBuffer: Threadsafe
That was all. According to Tiger who introduced StringBuilder "Do you need thread safety for string concatenation?" It seems that it was made with the idea.
String Manipulation Threadsafe vs Performance StringBuilder
Certainly I have never seen string concatenation that requires thread safety. It's a place that many people used without much concern, but I think it's amazing to catch it.
I tried to verify it easily. We have adopted this verification method. [Java] I tried to verify the difference between StringBuilder and StringBuffer from the viewpoint of thread safety
Share the same StringBuilder / StringBuffer instance with 100 threads and repeat append 10,000 times. As a result, we are seeing if length () is correctly 1 million. The processing speed was also measured.
StringBuilderTest.java
public class StringBuilderTest {
public static void main (String... args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(100);
StringBuilder sb = new StringBuilder();
List<BuilderTask> taskList = new ArrayList<BuilderTask>();
for(int i = 0; i < 100; i++) {
taskList.add(new BuilderTask(sb));
}
Long start = System.currentTimeMillis();
executor.invokeAll(taskList);
System.out.println("-----StringBuilder-----");
System.out.println("TAT : " + (System.currentTimeMillis() - start));
System.out.println("length() : " + sb.length());
}
static class BuilderTask implements Callable<Integer> {
private StringBuilder sb;
public BuilderTask(StringBuilder sb) {
this.sb = sb;
}
@Override
public Integer call() throws Exception {
for(int i = 0; i < 10000; i++) {
sb.append("#");
}
return null;
}
}
}
StringBufferTest.java
public class StringBufferTest {
public static void main (String... args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(100);
StringBuffer sb = new StringBuffer();
List<BufferTask> taskList = new ArrayList<BufferTask>();
for(int i = 0; i < 100; i++) {
taskList.add(new BufferTask(sb));
}
Long start = System.currentTimeMillis();
executor.invokeAll(taskList);
System.out.println("-----StringBuffer-----");
System.out.println("TAT : " + (System.currentTimeMillis() - start));
System.out.println("length() : " + sb.length());
}
static class BufferTask implements Callable<Integer> {
private StringBuffer sb;
public BufferTask(StringBuffer sb) {
this.sb = sb;
}
@Override
public Integer call() throws Exception {
for(int i = 0; i < 10000; i++) {
sb.append("#");
}
return null;
}
}
}
-----StringBuilder-----
TAT : 32
length() : 542861
-----StringBuffer-----
TAT : 157
length() : 1000000
With StringBuilder, almost half of the characters were lost. Instead, the processing speed is about 5 times faster.
It's hard to imagine when thread-safe string concatenation is needed in real business, so I think I'll use StringBuilder with a focus on performance.
Recommended Posts