性能测试
在日常开发中,常用的字符串拼接主要有String +、String.concat、StringBuffer.append以及StringBuilder.append等四种。接下来我们测试一下这四种方式的性能到底哪个强。
- 背景:对字符串进行5000次的拼接操作。
- 运行环境:
- CPU:2.4 GHz 四核Intel Core i5
- 内存:16G
- 系统:macOS 10.15.5
- jdk:1.8.0_211
- 测试工具:junitPerf
1.String +
//String +
@Test
@JunitPerfConfig(threads = 1,warmUp = 1000,duration = 2000)
public void stringDemo(){
String string = "";
for (int i = 0; i < BASE_NUM; i++) {
string += "123";
}
}
2.String.concat
//String concat
@Test
@JunitPerfConfig(threads = 1,warmUp = 1000,duration = 2000)
public void stringConcatDemo(){
String string = "";
for (int i = 0; i < BASE_NUM; i++) {
string = string.concat("123");
}
}
3.StringBuffer.append
//StringBuffer append
@Test
@JunitPerfConfig(threads = 1,warmUp = 1000,duration = 2000)
public void stringBufferDemo(){
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < BASE_NUM; i++) {
stringBuffer.append("123");
}
}
4.StringBuuilder.append
//StringBuilder append
@Test
@JunitPerfConfig(threads = 1,warmUp = 1000,duration = 2000)
public void stringBuilderDemo(){
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < BASE_NUM; i++) {
stringBuilder.append("123");
}
}
测试结果:
1.String +
[INFO] [2020-07-12 14:45:52.655] [c.l.a.s.Demo.stringDemo] - --------------------------------------------------------
[INFO] [2020-07-12 14:45:52.656] [c.l.a.s.Demo.stringDemo] - Started at: 2020-07-12 14:45:50.590
[INFO] [2020-07-12 14:45:52.656] [c.l.a.s.Demo.stringDemo] - Invocations: 124
[INFO] [2020-07-12 14:45:52.656] [c.l.a.s.Demo.stringDemo] - Success: 124
[INFO] [2020-07-12 14:45:52.657] [c.l.a.s.Demo.stringDemo] - Errors: 0
[INFO] [2020-07-12 14:45:52.657] [c.l.a.s.Demo.stringDemo] - Thread Count: 1
[INFO] [2020-07-12 14:45:52.657] [c.l.a.s.Demo.stringDemo] - Warm up: 1000ms
[INFO] [2020-07-12 14:45:52.658] [c.l.a.s.Demo.stringDemo] - Execution time: 2000ms
[INFO] [2020-07-12 14:45:52.658] [c.l.a.s.Demo.stringDemo] - Throughput: 124/s (Required: -1/s) - PASSED
[INFO] [2020-07-12 14:45:52.658] [c.l.a.s.Demo.stringDemo] - Memory cost: 16byte
[INFO] [2020-07-12 14:45:52.664] [c.l.a.s.Demo.stringDemo] - Min latency: 7.164541ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:45:52.665] [c.l.a.s.Demo.stringDemo] - Max latency: 115.50648ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:45:52.665] [c.l.a.s.Demo.stringDemo] - Avg latency: 16.036825ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:45:52.665] [c.l.a.s.Demo.stringDemo] - --------------------------------------------------------
2.String.concat
[INFO] [2020-07-12 14:47:12.577] [c.l.a.s.Demo.stringConcatDemo] - --------------------------------------------------------
[INFO] [2020-07-12 14:47:12.578] [c.l.a.s.Demo.stringConcatDemo] - Started at: 2020-07-12 14:47:10.535
[INFO] [2020-07-12 14:47:12.578] [c.l.a.s.Demo.stringConcatDemo] - Invocations: 192
[INFO] [2020-07-12 14:47:12.578] [c.l.a.s.Demo.stringConcatDemo] - Success: 192
[INFO] [2020-07-12 14:47:12.579] [c.l.a.s.Demo.stringConcatDemo] - Errors: 0
[INFO] [2020-07-12 14:47:12.579] [c.l.a.s.Demo.stringConcatDemo] - Thread Count: 1
[INFO] [2020-07-12 14:47:12.579] [c.l.a.s.Demo.stringConcatDemo] - Warm up: 1000ms
[INFO] [2020-07-12 14:47:12.580] [c.l.a.s.Demo.stringConcatDemo] - Execution time: 2000ms
[INFO] [2020-07-12 14:47:12.580] [c.l.a.s.Demo.stringConcatDemo] - Throughput: 192/s (Required: -1/s) - PASSED
[INFO] [2020-07-12 14:47:12.581] [c.l.a.s.Demo.stringConcatDemo] - Memory cost: 16byte
[INFO] [2020-07-12 14:47:12.586] [c.l.a.s.Demo.stringConcatDemo] - Min latency: 6.198077ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:47:12.587] [c.l.a.s.Demo.stringConcatDemo] - Max latency: 32.883385ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:47:12.587] [c.l.a.s.Demo.stringConcatDemo] - Avg latency: 10.378245ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:47:12.587] [c.l.a.s.Demo.stringConcatDemo] - --------------------------------------------------------
3.StringBuffer.append
[INFO] [2020-07-12 14:47:34.747] [c.l.a.s.Demo.stringBufferDemo] - --------------------------------------------------------
[INFO] [2020-07-12 14:47:34.748] [c.l.a.s.Demo.stringBufferDemo] - Started at: 2020-07-12 14:47:32.698
[INFO] [2020-07-12 14:47:34.748] [c.l.a.s.Demo.stringBufferDemo] - Invocations: 53224
[INFO] [2020-07-12 14:47:34.748] [c.l.a.s.Demo.stringBufferDemo] - Success: 53224
[INFO] [2020-07-12 14:47:34.749] [c.l.a.s.Demo.stringBufferDemo] - Errors: 0
[INFO] [2020-07-12 14:47:34.749] [c.l.a.s.Demo.stringBufferDemo] - Thread Count: 1
[INFO] [2020-07-12 14:47:34.749] [c.l.a.s.Demo.stringBufferDemo] - Warm up: 1000ms
[INFO] [2020-07-12 14:47:34.749] [c.l.a.s.Demo.stringBufferDemo] - Execution time: 2000ms
[INFO] [2020-07-12 14:47:34.750] [c.l.a.s.Demo.stringBufferDemo] - Throughput: 53224/s (Required: -1/s) - PASSED
[INFO] [2020-07-12 14:47:34.750] [c.l.a.s.Demo.stringBufferDemo] - Memory cost: 16byte
[INFO] [2020-07-12 14:47:34.757] [c.l.a.s.Demo.stringBufferDemo] - Min latency: 0.024695ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:47:34.759] [c.l.a.s.Demo.stringBufferDemo] - Max latency: 4.160009ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:47:34.761] [c.l.a.s.Demo.stringBufferDemo] - Avg latency: 0.037200477ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:47:34.761] [c.l.a.s.Demo.stringBufferDemo] - --------------------------------------------------------
4.StringBuilder.append
[INFO] [2020-07-12 14:48:08.912] [c.l.a.s.Demo.stringBuilderDemo] - --------------------------------------------------------
[INFO] [2020-07-12 14:48:08.913] [c.l.a.s.Demo.stringBuilderDemo] - Started at: 2020-07-12 14:48:06.865
[INFO] [2020-07-12 14:48:08.913] [c.l.a.s.Demo.stringBuilderDemo] - Invocations: 53838
[INFO] [2020-07-12 14:48:08.913] [c.l.a.s.Demo.stringBuilderDemo] - Success: 53838
[INFO] [2020-07-12 14:48:08.914] [c.l.a.s.Demo.stringBuilderDemo] - Errors: 0
[INFO] [2020-07-12 14:48:08.914] [c.l.a.s.Demo.stringBuilderDemo] - Thread Count: 1
[INFO] [2020-07-12 14:48:08.914] [c.l.a.s.Demo.stringBuilderDemo] - Warm up: 1000ms
[INFO] [2020-07-12 14:48:08.915] [c.l.a.s.Demo.stringBuilderDemo] - Execution time: 2000ms
[INFO] [2020-07-12 14:48:08.915] [c.l.a.s.Demo.stringBuilderDemo] - Throughput: 53838/s (Required: -1/s) - PASSED
[INFO] [2020-07-12 14:48:08.915] [c.l.a.s.Demo.stringBuilderDemo] - Memory cost: 16byte
[INFO] [2020-07-12 14:48:08.924] [c.l.a.s.Demo.stringBuilderDemo] - Min latency: 0.024493ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:48:08.926] [c.l.a.s.Demo.stringBuilderDemo] - Max latency: 4.138278ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:48:08.928] [c.l.a.s.Demo.stringBuilderDemo] - Avg latency: 0.0367233ms (Required: -1.0ms) - PASSED
[INFO] [2020-07-12 14:48:08.928] [c.l.a.s.Demo.stringBuilderDemo] - --------------------------------------------------------
- 从测试结果可以看出,在拼接性能上StringBuilder.append > StringBuffer.append >> String.concat > String +。
源码分析
虽然我们得出了结论,那为什么会这样的?接下来我们从源码上分析。
- 首先,我们先分析一下“+”,其实“+”是一个语法糖,要知道它底层是怎么实现的,就要借助反编译工具了。
- 反编译工具:crf
对stringDemo方法进行反编译之后的代码。
java -jar cfr-0.150.jar ./Demo.class --methodname stringDemo --stringbuilder false
@Test
@JunitPerfConfig(threads=1, warmUp=1000L, duration=2000L)
public void stringDemo() {
String string = "";
for (int i = 0; i < 5000; ++i) {
string = new StringBuilder().append(string).append("123").toString();
}
}
可以看出,“+”其实是new StringBuilder,然后调用的appen(),也就是说,每进行一次拼接,就要new 一个StringBuilder对象,这是多么大的消耗啊!所以“+”进行拼接性能是最差的。
- 然后对String.concat进行分析。
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
concat会创建一个已有字符串和带拼接字符串长度之和的数组,并将两个数组复制到新数组,在用新数组创建一个String对象。创建String对象保证的字符串的不可变性,为什么?因为String中的char[]使用final修饰的。
3.接下来就是append了,StringBuffer和StringBuilder的实现原理相似,不同在于StringBuffer的append方法用synchronized修饰,而StringBuilder没有。这就是为什么StringBuffer是线程安全,而StringBuilder不是线程安全的原因了,所以说StringBuilder.append性能会比StringBuffer.append好一点点。
StringBuffer.append
@Override
public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);
return this;
}
StringBuilder.append
@Override
public StringBuilder append(String str) {
super.append(str);
return this;
}
继续分析,StringBuffer和StringBuilder都是可变的字符串,他们都继承AbstractStringBuilder,而AbstractStringBuilder 的成员数组char[]是没有final修饰的。
在拼接过程中,char[]会根据已有字符串长度和带拼接字符串长度进行扩容,然后再将数组复制到成员数组中(char [] value)
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
//扩容
ensureCapacityInternal(count + len);
//拷贝数组
str.getChars(0, len, value, count);
count += len;
return this;
}
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
//拷贝数组操作
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
分析源码后可以发现,StringBuffer和StringBuilder的append过程中,并没有创建对象,而是更加底层的数组操作,这就是为什么性能高的原因了。
总结
拼接方式这么多,而且性能差别这么大,在使用上有什么要注意的呢?
1.对于少量的拼接操作,用+(在拼接次数不多的情况下,性能差距不明显,怎么方便怎么来)
2.在for循环(大量拼接操作)下使用StringBuffer或者StringBuilder
3.并发场景下使用StringBuffer(线程安全)。
– from 常用的字符串拼接方式性能分析以及源码分析