jdk1.8 环境下;
“+”与StringBuilder拼接字符串10万次,效率差别可不是一点点哦!!!
差了几千倍,小本本记下来;
long time1 = System.currentTimeMillis();
String result = "";
for (int i = 0; i < 100000; i++) {
result += "六六六";
}
long time2 = System.currentTimeMillis();
System.out.println("“+”拼接字符串100000次,耗时(毫秒)" + (time2-time1));
time1 = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++) {
sb.append("六六六");
}
time2 = System.currentTimeMillis();
System.out.println("StringBuilder拼接字符串100000次,耗时(毫秒)" + (time2-time1));