public class TestA {
public static void main(String[] args) {
long stringBeginTime=System.currentTimeMillis();
String s="";
for(int i=1;i<=10000;i++)
{
s=s+"hahaha 我作业快写完了";
}
long stringEndTime=System.currentTimeMillis();
long stringBufferBeginTime=System.currentTimeMillis();
StringBuffer sb=new StringBuffer();
for(int i=1;i<=10000;i++)
{
sb.append("hehehe 学渣写没卵用");
}
long stringBufferEndTime=System.currentTimeMillis();
System.out.println("String运行时间为"+(stringEndTime-stringBeginTime));
System.out.println("StringBuffer运行时间为"+(stringBufferEndTime-stringBufferBeginTime));
}
}
输出:
String运行时间为865
StringBuffer运行时间为1