String、StringBuilder、StringBuffer對比

參考:http://swiftlet.net/archives/1694html

http://www.cnblogs.com/springcsc/archive/2009/12/03/1616326.htmlspring

 

String  字符串(character strings),不是線程安全的swift

StringBuilder  非線程安全的字符串序列(A mutable sequence of characters)安全

StringBuffer  線程安全的字符串序列(A thread-safe, mutable sequence of characters)app

一、初始化方式

  String    ide

String str="abc";
String str1=new String("abc");

  這兩種初始化的方式的差異是:後者產生了兩個對象,內存浪費。性能

  StringBuffer測試

StringBuffer sb1=new StringBuffer("abc");
StringBuffer sbf1=new StringBuffer();
sbf1.append("abc");            

  從源碼上沒看出來這兩種的明顯差別。有老司機明白的能夠指點一二。ui

  StringBuilderspa

  和StringBuffer的初始化方式是同樣的。

二、性能比較

  測試代碼  

package cn.simple.src_study;

public class StringStudy {
    public static void main(String[] args) {
        long strStart = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            String str = "abc";
        }
        long strEnd = System.currentTimeMillis();
        System.out.println("str=\"abc\"; 執行10000次耗時:" + (strEnd - strStart));

        long strStart1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            String str1 = new String("abc");
        }
        long strEnd1 = System.currentTimeMillis();
        System.out.println("str=new String(\"abc\"); 執行10000次耗時:" + (strEnd1 - strStart1));

        long sbfStart = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            StringBuffer sbf = new StringBuffer("abc");
        }
        long sbfEnd = System.currentTimeMillis();
        System.out.println("sbf=new StringBuffer(\"abc\"); 執行10000次耗時:" + (sbfEnd - sbfStart));

        long sbfStart1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            StringBuffer sbf1 = new StringBuffer();
            sbf1.append("abc");
        }
        long sbfEnd1 = System.currentTimeMillis();
        System.out.println("sbf.append(\"abc\"); 執行10000次耗時:" + (sbfEnd1 - sbfStart1));

        long sbdStart = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            StringBuilder sbd = new StringBuilder("abc");
        }
        long sbdEnd = System.currentTimeMillis();
        System.out.println("sbd=new StringBuilder(\"abc\"); 執行10000次耗時:" + (sbdEnd - sbdStart));

        long sbdStart1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            StringBuilder sbd = new StringBuilder();
            sbd.append("abc");
        }
        long sbdEnd1 = System.currentTimeMillis();
        System.out.println("sbd.append(\"abc\"); 執行10000次耗時:" + (sbdEnd1 - sbdStart1));

    }
}
View Code

  結果截圖

  從測試結果能夠看出:若是值是固定的直接賦值給字符串,效率最高。

  若是字符串存在拼接過程呢?

  測試代碼:

package cn.simple.src_study;

public class StringStudy2 {
    public static void main(String[] args) {
        long strStart = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            String str = "abc";
            str+="def";
        }
        long strEnd = System.currentTimeMillis();
        System.out.println("str=\"abc\"; 執行10000次耗時:" + (strEnd - strStart));

        long strStart1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            String str1 = new String("abc");
            str1+="edf";
        }
        long strEnd1 = System.currentTimeMillis();
        System.out.println("str=new String(\"abc\"); 執行10000次耗時:" + (strEnd1 - strStart1));

        long sbfStart = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {

            StringBuffer sbf = new StringBuffer("abc");
            sbf.append("edf");
        }
        long sbfEnd = System.currentTimeMillis();
        System.out.println("sbf=new StringBuffer(\"abc\"); 執行10000次耗時:" + (sbfEnd - sbfStart));

        long sbfStart1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            StringBuffer sbf1 = new StringBuffer();
            sbf1.append("abc");
            sbf1.append("edf");
        }
        long sbfEnd1 = System.currentTimeMillis();
        System.out.println("sbf.append(\"abc\"); 執行10000次耗時:" + (sbfEnd1 - sbfStart1));

        long sbdStart = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            StringBuilder sbd = new StringBuilder("abc");
            sbd.append("edf");
        }
        long sbdEnd = System.currentTimeMillis();
        System.out.println("sbd=new StringBuilder(\"abc\"); 執行10000次耗時:" + (sbdEnd - sbdStart));

        long sbdStart1 = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            StringBuilder sbd = new StringBuilder();
            sbd.append("abc");
            sbd.append("edf");
        }
        long sbdEnd1 = System.currentTimeMillis();
        System.out.println("sbd.append(\"abc\"); 執行10000次耗時:" + (sbdEnd1 - sbdStart1));

    }
}
View Code

  測試結果:

  從測試結果能夠看出:存在字符串拼接的狀況下性能StringBuilder>StringBuffer>String

相關文章
相關標籤/搜索