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

来源:互联网 发布:网页美工设计课件 编辑:程序博客网 时间:2024/06/10 18:56


  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

0 0
原创粉丝点击