撸代码的时候拼接字符串是很常见的,使用合适的拼接方式可以节约资源,优化性能,接下来我们进行一下比较。
private int num = 100;
public static void main(String[] args){
// String
string1();
// String.concat()
string2();
// StringBuilder
string3();
// StringBuffer
string4();
}
private void string1() {
long stringStartTime = System.currentTimeMillis();
String string = "比" + "一" + "比" + "谁" + "的" + "速" + "度" + "快";
for (int i = 0; i < num; i++) {
string += i;
}
Log.d("String Time", System.currentTimeMillis() - stringStartTime + "");
}
private void string2() {
long stringConcatStartTime = System.currentTimeMillis();
String stringConcat = "比".concat("一").concat("比").concat("谁").concat("的").concat("速").concat("度").concat("快");
for (int i = 0; i < num; i++) {
stringConcat.concat(String.valueOf(i));
}
Log.d("StringConcat Time", System.currentTimeMillis() - stringConcatStartTime + "");
}
private void string3() {
long stringBuilderStartTime = System.currentTimeMillis();
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("比").append("一").append("比").append("谁").append("的").append("速").append("度").append("快");
for (int i = 0; i < num; i++) {
stringBuilder.append(i);
}
stringBuilder.toString();
Log.d("StringBuilder Time", System.currentTimeMillis() - stringBuilderStartTime + "");
}
private void string4() {
long stringBufferStartTime = System.currentTimeMillis();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("比").append("一").append("比").append("谁").append("的").append("速").append("度").append("快");
for (int i = 0; i < num; i++) {
stringBuffer.append(i);
}
stringBuffer.toString();
Log.d("StringBuffer Time", System.currentTimeMillis() - stringBufferStartTime + "");
}
当 num 为 100 的时候四种方式基本无差,所以我们平时进行少量字符串拼接时候不用过分纠结拼接方式。
01-04 14:48:01.524 7636-7655/com.example.test D/String Time: 0
01-04 14:48:01.541 7636-7655/com.example.test D/StringConcat Time: 0
01-04 14:48:01.547 7636-7655/com.example.test D/StringBuilder Time: 0
01-04 14:48:01.553 7636-7655/com.example.test D/StringBuffer Time: 0
当我们把 num 改为 10000 ,显而易见拼接量比较大的时候 StringBuilder 和 StringBuffer 的效率要高出很多。
01-04 14:48:01.524 7636-7655/com.example.test D/String Time: 3810
01-04 14:48:01.541 7636-7655/com.example.test D/StringConcat Time: 17
01-04 14:48:01.547 7636-7655/com.example.test D/StringBuilder Time: 6
01-04 14:48:01.553 7636-7655/com.example.test D/StringBuffer Time: 6
我们继续加大 num 到 3000000,对 StringBuilder 和 StringBuffer 进行测试,我们发现其实并不向通常所说的 StringBuffer 因为有加锁效率会比 StringBuilder 低很多。所以在进行单线程大量数据拼接时候两者皆可,进行多线程拼接数据使用 StringBuffer 保证线程安全。
01-04 15:16:52.595 10691-10710/com.example.test D/StringBuilder Time: 2191
01-04 15:16:54.792 10691-10710/com.example.test D/StringBuffer Time: 2197
01-04 15:16:56.985 10691-10710/com.example.test D/StringBuilder Time: 2193
01-04 15:16:59.196 10691-10710/com.example.test D/StringBuffer Time: 2211
01-04 15:17:01.399 10691-10710/com.example.test D/StringBuilder Time: 2203
01-04 15:17:03.533 10691-10710/com.example.test D/StringBuffer Time: 2134
01-04 15:17:05.680 10691-10710/com.example.test D/StringBuilder Time: 2147
01-04 15:17:07.798 10691-10710/com.example.test D/StringBuffer Time: 2118
01-04 15:17:09.941 10691-10710/com.example.test D/StringBuilder Time: 2143
01-04 15:17:12.073 10691-10710/com.example.test D/StringBuffer Time: 2132
网友评论