/**
* @auto dh
* @create 2020-03-25-15:16
*/
public class StringTest {
public static void main(String[] args) {
long startTime = 0L;
long endTime = 0L;
String text = "";
StringBuffer sf = new StringBuffer("");
StringBuilder sl = new StringBuilder("");
/*StringBuffer效率测试*/
System.out.println("StringBuffer效率测试");
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
sf.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
/*StringBuilder效率测试*/
System.out.println("StringBuilder效率测试");
startTime=System.currentTimeMillis();
for(int i=0;i<20000;i++){
sl.append(String.valueOf(i));
}
endTime=System.currentTimeMillis();
System.out.println(endTime-startTime);
/*String效率测试*/
System.out.println("String效率测试");
startTime=System.currentTimeMillis();
for(int i=0;i<20000;i++){
text+=i;
}
endTime=System.currentTimeMillis();
System.out.println(endTime-startTime);
}
}