Java字符串——字符串生成器



  String str = "";
//  定义字符串的执行操作的其实时间
  long startTime = System.currentTimeMillis();
  for(int i= 0;i<10000;i++){
   str = str+i;
  }
  long endTime = System.currentTimeMillis();
  long time = endTime-startTime;
  System.out.println("system 消耗的时间为:"+time);
  
  StringBuilder builder = new StringBuilder("");
  startTime = System.currentTimeMillis();
  for(int i= 0;i<10000;i++){
   builder.append(i);
  }
  time = endTime-startTime;
  System.out.println("system 消耗的时间为:"+time);

运行效果如下:

system 消耗的时间为:468
system 消耗的时间为:0

你可能感兴趣的:(java)