Java 字符串拼接 五種方法的性能比較分析 從執行100次到90萬次

原文地址https://www.cnblogs.com/twzheng/p/5923642.html

字符串拼接一般使用“+”,但是“+”不能滿足大批量數據的處理,Java中有以下五種方法處理字符串拼接,各有優缺點,程序開發應選擇合適的方法實現。

  1. 加號 “+”

  2. String contact() 方法

  3. StringUtils.join() 方法

  4. StringBuffer append() 方法

  5. StringBuilder append() 方法
    由此可以看出:

  6. 方法1 加號 “+” 拼接 和 方法2 String contact() 方法 適用於小數據量的操作,代碼簡潔方便,加號“+” 更符合我們的編碼和閱讀習慣;

  7. 方法3 StringUtils.join() 方法 適用於將ArrayList轉換成字符串,就算90萬條數據也只需68ms,可以省掉循環讀取ArrayList的代碼;

  8. 方法4 StringBuffer append() 方法 和 方法5 StringBuilder append() 方法 其實他們的本質是一樣的,都是繼承自AbstractStringBuilder,效率最高,大批量的數據處理最好選擇這兩種方法。

  9. 方法1 加號 “+” 拼接 和 方法2 String contact() 方法 的時間和空間成本都很高(分析在本文末尾),不能用來做批量數據的處理。

    方法1
    public void testPlus() {
        System.out.println(">>> testPlus() <<<");
        String str = "";
        long start = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            str = str + "a";
        }
        long end = System.currentTimeMillis();
        long cost = end - start;
        System.out.println("   {str + \"a\"} cost=" + cost + " ms");
    }
方法2
   public void testConcat() {
        System.out.println(">>> testConcat() <<<");
        String str = "";
        long start = System.currentTimeMillis();
        for (int i = 0; i < max; i++) {
            str = str.concat("a");
        }
        long end = System.currentTimeMillis();
        long cost = end - start;
        System.out.println("   {str.concat(\"a\")} cost=" + cost + " ms");
    }

方法3
   public void testJoin() {
        System.out.println(">>> testJoin() <<<");
        long start = System.currentTimeMillis();
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < max; i++) {
            list.add("a");
        }
        long end1 = System.currentTimeMillis();
        long cost1 = end1 - start;
        StringUtils.join(list, "");
        long end = System.currentTimeMillis();
        long cost = end - end1;
        System.out.println("   {list.add(\"a\")} cost1=" + cost1 + " ms");
        System.out.println("   {StringUtils.join(list, \"\")} cost=" + cost
                + " ms");
    }
方法4
   public void testStringBuffer() {
        System.out.println(">>> testStringBuffer() <<<");
        long start = System.currentTimeMillis();
        StringBuffer strBuffer = new StringBuffer();
        for (int i = 0; i < max; i++) {
            strBuffer.append("a");
        }
        strBuffer.toString();
        long end = System.currentTimeMillis();
        long cost = end - start;
        System.out.println("   {strBuffer.append(\"a\")} cost=" + cost + " ms");
    }
方法5
   public void testStringBuilder() {
        System.out.println(">>> testStringBuilder() <<<");
        long start = System.currentTimeMillis();
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < max; i++) {
            strBuilder.append("a");
        }
        strBuilder.toString();
        long end = System.currentTimeMillis();
        long cost = end - start;
        System.out.println("   {strBuilder.append(\"a\")} cost=" + cost + " ms");
    }
  1. 字符串的加號“+” 方法, 雖然編譯器對其做了優化,使用StringBuilder的append方法進行追加,但是每循環一次都會創建一個StringBuilder對象,且都會調用toString方法轉換成字符串,所以開銷很大。
      注:執行一次字符串“+”,相當於 str = new StringBuilder(str).append(“a”).toString();
  2. 本文開頭的地方統計了時間開銷,根據上述分析再想想空間的開銷。常說拿空間換時間,反過來是不是拿時間換到了空間呢,但是在這裏,其實時間是消耗在了重複的不必要的工作上生成新的對象,toString方法),所以對大批量數據做處理時,加號“+” 和 contact 方法絕對不能用,時間和空間成本都很高。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章