String性能比较

zk3389 2013-07-26 08:07:26
public class Test {

public static void main(String[] args) {

final int SIZE = 10000;
long start = 0;

String str = new String();
start = System.currentTimeMillis();
for (int i = 0; i < SIZE; i++)
str += "aba"+i;
System.out.println(System.currentTimeMillis() - start);


String str2 = new String();
start = System.currentTimeMillis();
for (int i = 0; i < SIZE*100; i++)
str2.concat("abc"+i);
System.out.println(System.currentTimeMillis() - start);



StringBuffer sb = new StringBuffer();
start = System.currentTimeMillis();
for (int i = 0; i < SIZE*100; i++)
sb.append("abd"+i);
System.out.println(System.currentTimeMillis() - start);



StringBuilder sbu = new StringBuilder();
start = System.currentTimeMillis();
for (int i = 0; i < SIZE*100; i++)
sbu.append("abe"+i);
System.out.println(System.currentTimeMillis() - start);

}
}

输出:
813
218
250
172
几乎每次,concat的时间都比append少,这与理论不符啊?
...全文
371 10 打赏 收藏 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
nj_dobetter 2013-07-27
  • 打赏
  • 举报
回复
引用 8 楼 fudongrifdr 的回复:
我相信这篇文章绝对是你想要的http://www.blogjava.net/javagrass/archive/2010/01/24/310650.html
文章很牛!
oh_Maxy 2013-07-26
  • 打赏
  • 举报
回复
引用 5 楼 lcf 的回复:
你把str2.concat改成 str2=str2.concat。因为你没有东西接住str2.concat的返回值,jvm就将其优化掉了
顶~ str2.concat("abc"+i);会返回一个新的对象,类似str1+str2。 append每次都在同一个对象里操作。
lcf 2013-07-26
  • 打赏
  • 举报
回复
你把str2.concat改成 str2=str2.concat。因为你没有东西接住str2.concat的返回值,jvm就将其优化掉了
huntor 2013-07-26
  • 打赏
  • 举报
回复
StringBuilder/StringBuffer 在不断的调整长度。
        StringBuffer sb2 = new StringBuffer(3000000);
        start = System.currentTimeMillis();
        for (int i = 0; i < SIZE*100; i++)
            sb2.append("abd");
        System.out.println(System.currentTimeMillis() - start);

        StringBuilder sbu2 = new StringBuilder(3000000);
        start = System.currentTimeMillis();
        for (int i = 0; i < SIZE*100; i++)
            sbu2.append("abe");
        System.out.println(System.currentTimeMillis() - start);
这样就好多了
panzheguo 2013-07-26
  • 打赏
  • 举报
回复
学习学习
zk3389 2013-07-26
  • 打赏
  • 举报
回复
 String str2 = new String();
		 start = System.currentTimeMillis();
		 for (int i = 0; i < SIZE*100; i++)
			 str2.concat("abc");
		 System.out.println(System.currentTimeMillis() - start);
		

		
		StringBuffer sb = new StringBuffer();
		start = System.currentTimeMillis();
		for (int i = 0; i < SIZE*100; i++)
			sb.append("abd");
		System.out.println(System.currentTimeMillis() - start);
改成这样,为什么concat用的时间还是少些
Acesidonu 2013-07-26
  • 打赏
  • 举报
回复
sb.append("abd"+i); //不要这样拼接 sb.append("abd").append(i);
huntor 2013-07-26
  • 打赏
  • 举报
回复
看append的代码和concat的比较类似。
    public AbstractStringBuilder append(String str) {
        if (str == null) str = "null";
        int len = str.length();
        ensureCapacityInternal(count + len);
        str.getChars(0, len, value, count);
        count += len;
        return this;
    }
    public String concat(String str) {
        int otherLen = str.length();
        if (otherLen == 0) {
            return this;
        }
        int len = value.length;
        char buf[] = Arrays.copyOf(value, len + otherLen);
        str.getChars(buf, len);
        return new String(buf, true);
    }
末日哥 2013-07-26
  • 打赏
  • 举报
回复
我相信这篇文章绝对是你想要的http://www.blogjava.net/javagrass/archive/2010/01/24/310650.html
hippoppower 2013-07-26
  • 打赏
  • 举报
回复
引用 5 楼 lcf 的回复:
你把str2.concat改成 str2=str2.concat。因为你没有东西接住str2.concat的返回值,jvm就将其优化掉了
这都可以

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧