Good programmers java tutorial Share + append the number and efficiency

Good programmers java tutorial Share + number and append efficiency problem , preface :

For novice Java people, learning String time, there must be countless individuals and we said, "Try not to use the + string concatenation, efficiency is not good, you should use the append ", but the truth really is that right? Let's test it by the actual code + number and append efficiency issues.

Code Analysis

public class Demo {

    public static void main(String[] args){

        String str = "";

        long start = System.currentTimeMillis();

        for(int i=0; i<100000; i++)

            str += "a";

        long end = System.currentTimeMillis();

        System.out.println(end - start);

 

        StringBuilder sb = new StringBuilder();

        start = System.currentTimeMillis();

        for(int i=0; i<100000; i++)

            sb.append("a");

        str = sb.toString();

        end = System.currentTimeMillis();   

        System.out.println(end - start);

    }

}

We are in the loop respectively + good and append to the string loop stitching. Respectively print out their time:

Compiled results are as follows:

Picture 1.png

We can see that the efficiency is indeed the difference is very obvious. Do not worry, we continue to come through the Java the p- to see his file byte code instruction command:

Picture 2.png

We can find use + number string concatenation, in the body of the loop facade every time new a String Builder object and then call append method to append a string.

Here we look at the use append bytecode instructions mosaic:

Picture 3.png

To sum up we can find in the loop body which we use + number string concatenation, the compiler will give every time we create a Stringbuilder objects, which is the use of recycled inside the body + the root cause of poor performance number. Does this show that we should never use the + sign to concatenate strings Well? In fact, this is not entirely correct. Here we come to a set of tests:

Test code:

public class Test {

    public static void main(String[] args){

        String a = "a";

        String b = "a";

        String c = "a";

        String d = "a";

        String e = a + b + c;

        e += d;

    }

}

 

我们可以通过javap命令来查看以上字节码文件的指令如下:我们会发现只帮助我们new2StringBuilder对象。我们可以得到如下结论:对于此处的str = str + a”,编译器会处理为new StringBuilder().append(str).append(a),不管一次性+几个字符串,只要+拼接全部在一条语句中,就只会new一次,循环中+拼接被断成了十万条语句,那自然就会new十万次。

Picture 4.png

总结

We do not use the loop body + sign to concatenate strings, use append to replace, but not inside the body of the loop using + numbers will not affect the performance, even compiler also optimized. For herein STR STR + = " A ", the compiler will be treated as new new the StringBuilder (). The append (STR) .append ( " A " ) , regardless of the disposable + several strings, as long as the + splicing in one statement all it will only be new once, cycle + stitching is broken into one hundred thousand statement, it will naturally new thousands of times. Little friends, you GET new skills did?


Guess you like

Origin blog.51cto.com/14249543/2407439