先上结果
以上进行了两组测试(数据均来自真机测试)
放大一下stringBuffer和stringBuilder这两组数据
以上不难看出StringBuilder的性能明显优于StringBuffer
以上可以看出不考虑线程安全等因素下耗时如下
StringBuilder耗时>StringBuffer耗时>String耗时>String.format耗时
粗略的解释一下为什么
- 先说一下正常的字符串相“+”和“.format”之间的区别
.format在很多地方都有,主要用途是进行格式化的作用因此使用下面代码格式化肯定是没有直接叠加快的
s = String.format("%s%s", s1, s2);
-
在说一下啊说找一下String为什么慢
首先我们要知道String类是不可变类(String.format就不用说了本身就是String类的方法)而StringBuilder和StringBuffer是可变的
所以StringBuilder和StringBuffer优于String因为String叠加需要重新获取一块内存然后讲两段字符串相加在放入新的内存中
因此StringBuilder和StringBuffer可以明显在上面的柱状图总看出他俩比较牛逼 在分析一下StringBuilder和StringBuffer
小提示:StringBuffer 是线程安全的而StringBuilder并不是!!!
StringBuffer的所有公开方法都是同步的,StringBuilder是没有对方法加锁同步的。
所以理论上毫无疑问,StringBuilder 的性能要远大于 StringBuffer。
StringBuffer 和 StringBuilder 的区别
https://blog.csdn.net/changgebuyu/article/details/86523998
写一下我测试的代码
- activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="5">
</GridView>
</LinearLayout>
- MainActivity
package com.hrk.stringtest;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private ArrayList arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<String>();
arrayList.add("");
arrayList.add("String");
arrayList.add("stringBuffer");
arrayList.add("stringBuilder");
arrayList.add("string.format");
addList(100, 200);
addList(100, 500);
addList(100, 1000);
addList(10, 200);
addList(10, 500);
addList(10, 1000);
GridView gridView = findViewById(R.id.list);
gridView.setAdapter(new ArrayAdapter(this, R.layout.list_item, arrayList));
}
void addList(int length, int endLength) {
arrayList.add("叠加到 " + endLength + "(长度:" + length + ")");
String s = getRandomString(100);
int num = endLength / length;
arrayList.add(stringTest(num, s));
arrayList.add(stringBufferTest(num, s));
arrayList.add(stringBuilderTest(num, s));
arrayList.add(stringFormatTest(num, s));
}
String stringTest(int num, String s) {
long start = System.nanoTime();
String ends;
for (int i = 0; i < 100; i++) {
ends = "";
for (int j = 0; j < num; j++) {
ends += s;
}
}
long end = System.nanoTime();
DecimalFormat df = new DecimalFormat("0.000");
return df.format(Double.valueOf(end - start) / (100 * 1000 * 1000));
}
String stringBufferTest(int num, String s) {
long start = System.nanoTime();
StringBuffer stringBuffer = new StringBuffer(s);
StringBuffer endStringBuffer;
for (int i = 0; i < 100; i++) {
endStringBuffer = new StringBuffer();
for (int j = 0; j < num; j++) {
endStringBuffer.append(stringBuffer);
}
}
long end = System.nanoTime();
DecimalFormat df = new DecimalFormat("0.000");
return df.format(Double.valueOf(end - start) / (100 * 1000 * 1000));
}
String stringBuilderTest(int num, String s) {
long start = System.nanoTime();
StringBuilder stringBuilder = new StringBuilder(s);
StringBuilder endStringBuilder;
for (int i = 0; i < 100; i++) {
endStringBuilder = new StringBuilder();
for (int j = 0; j < num; j++) {
endStringBuilder.append(stringBuilder);
}
}
long end = System.nanoTime();
DecimalFormat df = new DecimalFormat("0.000");
return df.format(Double.valueOf(end - start) / (100 * 1000 * 1000));
}
String stringFormatTest(int num, String s) {
long start = System.nanoTime();
String ends;
for (int i = 0; i < 100; i++) {
ends = "";
for (int j = 0; j < num; j++) {
ends = String.format("%s%s", ends, s);
}
}
long end = System.nanoTime();
DecimalFormat df = new DecimalFormat("0.000");
return df.format(Double.valueOf(end - start) / (100 * 1000 * 1000));
}
public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
}
- list_item.xml
<?xml version="1.0" encoding="UTF-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:textSize="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>