public class Huhu {
public static void main(String[] args) {
Integer s0 = 0;
long startTime0 = System.currentTimeMillis();
while (true) {
s0 += 1;
if (s0 == 100000) {
long endTime0 = System.currentTimeMillis();
System.out.println(endTime0 - startTime0);
break;
}
}
String s1 = "";
long startTime1 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s1 +='a';
}
long endTime1 = System.currentTimeMillis();
System.out.println(endTime1 - startTime1);
StringBuffer s2 = new StringBuffer();
long startTime2 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s2 = s2.append("a");
}
long endTime2 = System.currentTimeMillis();
System.out.println(endTime2 - startTime2);
StringBuilder s3 = new StringBuilder();
long startTime3 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
s3 = s3.append("a");
}
long endTime3 = System.currentTimeMillis();
System.out.println(endTime3 - startTime3);
}
}
String StringBuffer StringBuilder性能比较
最新推荐文章于 2024-03-14 01:50:34 发布