StringBuilder、StringBuffer和+号的比较

来源:互联网 发布:合同登记软件 编辑:程序博客网 时间:2024/06/05 02:58

参考: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;    }}



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


原创粉丝点击