ITPub博客

首页 > Linux操作系统 > Linux操作系统 > string与stringbuffer的性能对比

string与stringbuffer的性能对比

原创 Linux操作系统 作者:mystart2012 时间:2012-11-21 15:41:06 0 删除 编辑

string与stringbuffer常见于java面试中,以下为string与strinbuffer性能对比示例的java代码 以下为执行5000次的字符串边接。

 

 

package com.buyli.interview.baseinfo;

/**
 * @Copyright @ 2012
 * All right reserved
 * @version 创建时间:Created on 2012-11-21
 * @author 作者:Create by
 * @Email: 360buyli@gmail.com
 * @description 用于测试string与StringBuffer的性能差异
 */

public class StringInterview {
 
 public static void main(String[] args) {
  String result="";
  StringBuffer sbResult=new StringBuffer();
  int count=5000;//代表连接字符串的次数
  
  long startTime=System.currentTimeMillis();
  for (int i = 0; i    result=result+"a";
  }
  System.out.println("string时间差:"+(System.currentTimeMillis()-startTime));
  
   startTime=System.currentTimeMillis();
  for (int i = 0; i    sbResult.append("a");
  }
  System.out.println("stringbuffer时间差:"+(System.currentTimeMillis()-startTime));
 }
}

示例结果:
 string时间差:32
stringbuffer时间差:0

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28225168/viewspace-749669/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: java实现验证码
下一篇: Math.round用法 .
请登录后发表评论 登录
全部评论

注册时间:2012-10-22

  • 博文量
    23
  • 访问量
    52009