关于chrome-devtools:StringBuider-在什么条件下如何使用效率更高

34次阅读

共计 17048 个字符,预计需要花费 43 分钟才能阅读完成。

引言

都说 StringBuilder 在解决字符串拼接上效率要强于 String,但有时候咱们的了解可能会存在肯定的偏差。最近我在测试数据导入效率的时候就发现我以前对 StringBuilder 的局部了解是谬误的。起初我通过实际测试 + 找原理 的形式搞清楚了这块的逻辑。当初将过程分享给大家

测试用例

咱们的代码在循环中拼接字符串个别有两种状况

  • 第一种就是每次循环将对象中的几个字段拼接成一个新字段,再赋值给对象
  • 第二种操作是在循环外创立一个字符串对象,每次循环向该字符串拼接新的内容。循环完结后失去拼接好的字符串

对于这两种状况,我创立了两个对照组

第一组:

在每次 For 循环中拼接字符串,即拼即用、用完即毁。别离应用 String 和 StringBuilder 拼接

 /**
     * 循环内 String 拼接字符串,一次循环后销毁
     */
    public static void useString(){for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {String str = str1 + i + str2 + i + str3 + i + str4 ;}
    }

    /**
     * 循环内 应用 StringBuilder 拼接字符串,一次循环后销毁
     */
    public static void useStringBuilder(){for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {StringBuilder sb = new StringBuilder();
            String s = sb.append(str1).append(i).append(str2).append(i).append(str3).append(i).append(str4).toString();}
    } 

第二组:

屡次 For 循环拼接一个字符串,循环完结后应用字符串,应用后由垃圾回收器回收。也是别离应用 String 和 StringBuilder 拼接

 /**
     * 屡次循环拼接成一个字符串 用 String
     */
    public static void useStringSpliceOneStr (){
        String str = "";
        for (int i = 0; i < CYCLE_NUM_LOWER; i++) {str += str1 + str2 + str3 + str4 + i;}
    }

    /**
     * 屡次循环拼接成一个字符串 用 StringBuilder
     */
    public static void useStringBuilderSpliceOneStr(){StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CYCLE_NUM_LOWER; i++) {sb.append(str1).append(str2).append(str3).append(str4).append(i);
        }
    } 

为了保障测试品质,在每个测试项目进行前。线程劳动 2s,之后空跑 5 次热身。最初执行 5 次求均匀工夫的形式计算工夫

 public static int executeSometime(int kind, int num) throws InterruptedException {Thread.sleep(2000);
        int sum = 0;
        for (int i = 0; i < num + 5; i++) {long begin = System.currentTimeMillis();

            switch (kind){
                case 1:
                    useString();
                    break;
                case 2:
                    useStringBuilder();
                    break;
                case 3:
                    useStringSpliceOneStr();
                    break;
                case 4:
                    useStringBuilderSpliceOneStr();
                    break;
                default:
                    return 0;
            }

            long end = System.currentTimeMillis();

            if(i > 5){sum += (end - begin);
            }
        }
        return sum / num;
    } 

主办法

public class StringTest {
    public static final int CYCLE_NUM_BIGGER = 10_000_000;
    public static final int CYCLE_NUM_LOWER = 10_000;
    public static final String str1 = "张三";
    public static final String str2 = "李四";
    public static final String str3 = "王五";
    public static final String str4 = "赵六";


    public static void main(String[] args) throws InterruptedException {
        int time = 0;
        int num = 5;

        time = executeSometime(1, num);
        System.out.println("String 拼接"+ CYCLE_NUM_BIGGER +"次," + num + "次均匀工夫:" + time + "ms");

        time = executeSometime(2, num);
        System.out.println("StringBuilder 拼接"+ CYCLE_NUM_BIGGER +"次," + num + "次均匀工夫:" + time + "ms");

        time = executeSometime(3, num);
        System.out.println("String 拼接单个字符串"+ CYCLE_NUM_LOWER +"次," + num + "次均匀工夫:" + time + "ms");

        time = executeSometime(4, num);
        System.out.println("StringBuilder 拼接单个字符串"+ CYCLE_NUM_LOWER +"次," + num + "次均匀工夫:" + time + "ms");

    }
} 

测试后果

测试后果如下

后果剖析

第一组

10_000_000 次循环拼接,在循环内应用 String 和 StringBuilder 的效率是一样的!为什么呢?

应用 javap -c StringTest.class 反编译查看两个办法编译后的文件:

能够发现 String 办法拼接字符串编译器优化后应用的就是 StringBuilder、因而用例 1 和用例 2 的效率是一样的。

第二组

第二组的后果就是大家脍炙人口的了,因为 10_000_000 次循环 String 拼接切实太慢所以我采纳了 10_000 次拼接来剖析。

剖析用例 3:尽管编译器会对 String 拼接做优化,然而它每次在循环内创立 StringBuilder 对象,在循环内销毁。下次循环他有创立。相比拟用例 4 在循环外创立,多了 n 次 new 对象、销毁对象的操作、n – 1 次将 StringBuilder 转换成 String 的操作。效率低也是理所应当了。

扩大

第一组的测试还有一种写法:

 /**
     * 循环内 应用 StringBuilder 拼接字符串,一次循环后销毁
     */
    public static void useStringBuilderOut(){StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {//            sb.setLength(0);
            sb.delete(0, sb.length());
            String s = sb.append(str1).append(i).append(str2).append(i).append(str3).append(i).append(str4).toString();}
    } 

循环外创立 StringBuilder 每次循环开始的时候清空 StringBuilder 的内容而后拼接。这种写法无论应用 sb.setLength(0); 还是 sb.delete(0, sb.length()); 效率都比间接在循环内应用 String / StringBuilder 慢。奈何满腹经纶我始终想不明确为什么他慢。我猜想是 new 对象的速度比重置长度慢,于是这样测试了以下:

 public static void createStringBuider() {for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {StringBuilder sb = new StringBuilder();
        }
    }

    public static void cleanStringBuider() {StringBuilder sb = new StringBuilder();
        for (int i = 0; i < CYCLE_NUM_BIGGER; i++) {sb.delete(0, sb.length());
        }
    } 

然而后果是 cleanStringBuider 更快。让我摸不着头脑
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qqzul2a6/y…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/qrpin3u9/d…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/jdzce2g3/c…
https://github.com/sz7epigs/v…
https://github.com/qvbuqqvw/n…
https://github.com/lty6dp09/i…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/jdzce2g3/c…
https://github.com/qqzul2a6/y…
https://github.com/lty6dp09/i…
https://github.com/qrpin3u9/d…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/lty6dp09/i…
https://github.com/qqzul2a6/y…
https://github.com/qvbuqqvw/n…
https://github.com/sz7epigs/v…
https://github.com/jdzce2g3/c…
https://github.com/lty6dp09/i…
如果有大神看到心愿能够帮忙剖析剖析

论断

  • 编译器会将 String 拼接优化成应用 StringBuilder,然而还是有一些缺点的。次要体现在循环内应用字符串拼接,编译器不会创立单个 StringBuilder 以复用
  • 对于屡次循环内拼接一个字符串的需要:StringBuilder 很快,因为其防止了 n 次 new 对象、销毁对象的操作,n – 1 次将 StringBuilder 转换成 String 的操作
  • StringBuilder 拼接不适用于循环内每次拼接即用的操作形式。因为编译器优化后的 String 拼接也是应用 StringBuilder 两者的效率一样。后者写起来还不便 …

正文完
 0