StringBuilder、StringBuffer和+号的比较

参考:http://bsr1983.iteye.com/blog/1935856

改了下深入分析Java使用+和StringBuilder进行字符串拼接的差异 的demo,发现+号的性能会更好

以下两份代码的差别在于new StringBuilder在循环中

代码:(StringBuilder性能好的情况)

/**
 * Created by loongmoon on 17/7/23.
 */
public class StringCatTest {
    public static void main(String[] args) {
        printResult(100);
        System.out.println("***********************************************");
        printResult(1000);
        System.out.println("***********************************************");
        printResult(10000);
        System.out.println("***********************************************");
        printResult(100000);
        System.out.println("***********************************************");
        printResult(1000000);
        System.out.println("***********************************************");
        printResult(10000000);



    }
    public static void printResult(long loopCount) {
        System.out.println("loopCount:" + loopCount);
        stringCat(loopCount);
        stringBuilderAppend(loopCount);
        stringBufferAppend(loopCount);
    }
    public static long stringCat(long loopCount) {
        long beginTime = System.currentTimeMillis();
        String str = "hello,world!";
        String result = "";

        for (int i = 0; i < loopCount; i++) {
            result += str;
        }
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("String cat time:" + consumeTime);
        return consumeTime;
    }

    public static long stringBuilderAppend(long loopCount) {
        long beginTime = System.currentTimeMillis();
        String str = "hello, world!";
        String result = "";
        StringBuilder stringBuilder = new StringBuilder();

        for (int i = 0; i < loopCount; i++) {
            stringBuilder.append(str);
        }
        result = stringBuilder.toString();
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("StringBuilder append time:" + consumeTime);
        return consumeTime;

    }

    public static long stringBufferAppend(long loopCount) {
        long beginTime = System.currentTimeMillis();
        String str = "hello, world!";
        String result = "";
        StringBuffer stringBuffer = new StringBuffer();

        for (int i = 0; i < loopCount; i++) {
            stringBuffer.append(str);
        }
        result = stringBuffer.toString();
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("StringBuffer append time:" + consumeTime);
        return consumeTime;
    }
}

代码:(+号性能好的情况)

/**
 * Created by loongmoon on 17/7/23.
 */
public class StringCatTest {
    public static void main(String[] args) {
        printResult(100);
        System.out.println("***********************************************");
        printResult(1000);
        System.out.println("***********************************************");
        printResult(10000);
        System.out.println("***********************************************");
        printResult(100000);
        System.out.println("***********************************************");
        printResult(1000000);
        System.out.println("***********************************************");
        printResult(10000000);



    }
    public static void printResult(long loopCount) {
        System.out.println("loopCount:" + loopCount);
        stringCat(loopCount);
        stringBuilderAppend(loopCount);
        stringBufferAppend(loopCount);
    }
    public static long stringCat(long loopCount) {
        long beginTime = System.currentTimeMillis();
        

        for (int i = 0; i < loopCount; i++) {
            String str = "hello,world!";
            String result = "";
            result += str;
        }
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("String cat time:" + consumeTime);
        return consumeTime;
    }

    public static long stringBuilderAppend(long loopCount) {
        long beginTime = System.currentTimeMillis();
        

        for (int i = 0; i < loopCount; i++) {
            String str = "hello, world!";
            String result = "";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(str);
        }
//        result = stringBuilder.toString();
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("StringBuilder append time:" + consumeTime);
        return consumeTime;

    }

    public static long stringBufferAppend(long loopCount) {
        long beginTime = System.currentTimeMillis();
        

        for (int i = 0; i < loopCount; i++) {
            String str = "hello, world!";
            String result = "";
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(str);
        }
//        result = stringBuffer.toString();
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("StringBuffer append time:" + consumeTime);
        return consumeTime;
    }
}


package packagetest;

/**
 * Created by loongmoon on 17/7/23.
 */
public class StringCatTest2 {
    public static void main(String[] args) {
        printResult(100);
        System.out.println("***********************************************");
        printResult(1000);
        System.out.println("***********************************************");
        printResult(10000);
        System.out.println("***********************************************");
        printResult(100000);
        System.out.println("***********************************************");
        printResult(1000000);
        System.out.println("***********************************************");
        printResult(10000000);



    }
    public static void printResult(long loopCount) {
        System.out.println("loopCount:" + loopCount);
        stringCat(loopCount);
        stringBuilderAppend(loopCount);
        stringBufferAppend(loopCount);
    }
    public static long stringCat(long loopCount) {
        long beginTime = System.currentTimeMillis();


        for (int i = 0; i < loopCount; i++) {
            String str = "This is only a" + " simple" + " test";
        }
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("String cat time:" + consumeTime);
        return consumeTime;
    }

    public static long stringBuilderAppend(long loopCount) {
        long beginTime = System.currentTimeMillis();


        for (int i = 0; i < loopCount; i++) {
            StringBuilder builder = new StringBuilder("This is only a").append(" simple").append(" test");
        }
//        result = stringBuilder.toString();
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("StringBuilder append time:" + consumeTime);
        return consumeTime;

    }

    public static long stringBufferAppend(long loopCount) {
        long beginTime = System.currentTimeMillis();


        for (int i = 0; i < loopCount; i++) {
            StringBuffer builder = new StringBuffer("This is only a").append(" simple").append(" test");
        }
//        result = stringBuffer.toString();
        long consumeTime = System.currentTimeMillis()-beginTime;
        System.out.println("StringBuffer append time:" + consumeTime);
        return consumeTime;
    }
}



代码(循环中只是对两个字符串相加。二者效率差不多)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值