标签:stringbuilder stringbuffer string java append
当我们仅仅需要a+b 的时候,两个字符串链接任何方法的效率基本一样,都在0.0001毫秒内就可以完成。不过如果需要1万次,10000万次,就会发现string自身的join速度显著下降
package com.java.lang; public class StringTest { int MAX = 10000; //1万次累加 public String Buffer(){ StringBuffer sb = new StringBuffer(); for(int i = 0; i < MAX;i++){ sb.append("abc"); } return sb.toString(); } public String Build(){ StringBuilder sb = new StringBuilder(); for(int i = 0; i < MAX;i++){ sb.append("abc"); } return sb.toString(); } public String StringJoin(){ String a = ""; for(int i = 0; i < MAX;i++){ a += "abc"; } return a; } public static void main(String[] args) { StringTest st = new StringTest(); long t1,t2,t; t1 = System.currentTimeMillis(); st.Buffer(); t2 = System.currentTimeMillis(); t = t2 - t1; System.out.println("StringBuffer 时间"+t); t1 = System.currentTimeMillis(); st.Build(); t2 = System.currentTimeMillis(); t = t2 - t1; System.out.println("StringBuild 时间"+t); t1 = System.currentTimeMillis(); st.StringJoin(); t2 = System.currentTimeMillis(); t = t2 - t1; System.out.println("String 自身链接 时间"+t); } }
java StringBuffer,StringBuilder,String自身连接效率对比,布布扣,bubuko.com
java StringBuffer,StringBuilder,String自身连接效率对比
标签:stringbuilder stringbuffer string java append
原文地址:http://blog.csdn.net/ghjhot/article/details/26268751