This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
字符串拼接汇总
feilong edited this page Feb 7, 2018
·
3 revisions
在开发过程中,经常会和字符串打交道, 其中字符串拼接的工作必不可少,
最常用的有
-
+
-
string concat
-
String.join (since jdk8)
-
StringBuffer append
-
StringBuilder append
我们来看看性能情况
public class StringAppendTest extends AbstractPerformanceTest{
private static final int LOOP_COUNT = 100_0000;
//---------------------------------------------------------------
@Override
protected String[] buildFieldArray(){
return toArray("+", "String concat", "String.join", "StringBuffer.append", "StringBuilder.append");
}
//---------------------------------------------------------------
@Override
protected ProcessCallback buildMutiCallback(){
return new ProcessCallback(){
@Override
public void processResult(String field){
switch (field) {
case "+":
String a = "a";
for (int i = 0; i < LOOP_COUNT; i++){
a += "b";
}
break;
case "String concat":
String c = "c";
for (int i = 0; i < LOOP_COUNT; i++){
c = c.concat("d");
}
break;
//since 20180207
case "String.join":
String j = "j";
for (int i = 0; i < LOOP_COUNT; i++){
j = String.join(j, "j");
}
break;
case "StringBuffer.append":
StringBuffer e = new StringBuffer("e");
for (int i = 0; i < LOOP_COUNT; i++){
e.append("d");
}
break;
case "StringBuilder.append":
StringBuilder d = new StringBuilder("e");
for (int i = 0; i < LOOP_COUNT; i++){
d.append("d");
}
break;
default:
break;
}
}
};
}
}