- public class StringBuilderDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- //
- // StringBuilder buf=new StringBuilder();
- // buf.append("李敖").append(":").
- // append("是著名的国学大师,前妻是胡女士").
- // insert(0, "大师").replace(1, 2, "牛人");
- // String str=buf.toString();
- // System.out.println(str);
- System.out.println(testString(100000));
- System.out.println(testStringBuilder(100000));
- }
- //计算:使用string 的连接运算(+)连接指定次数
- // 的字符串性能
- public static long testString (int times){
- //毫秒数
- long start = System.currentTimeMillis();
- String s="";
- for(int i=0;i<times;i++){
- s+="a";
- }
- long end = System.currentTimeMillis();
- return end-start;
- }
- public static long testStringBuilder(int times){
- long start = System.currentTimeMillis();
- StringBuilder buf=new StringBuilder();
- for(int i=0;i<times;i++){
- buf.append("a");
- }
- //System.out.println(buf.toString());
- long end = System.currentTimeMillis();
- return end-start;
- }
- }
复制代码
|
|