Java string concatenation Why use StringBuilder instead of string concatenation

Source: Internet
Author: User

Reference Link: http://tutorials.jenkov.com/java/strings.html

Often see on the net or around hear someone say string concatenation does not add directly with string, StringBuilder efficiency is higher than string concatenation. Others often say that StringBuffer is synchronous (thread-safe), StringBuilder is not thread-safe, and synchronization brings performance costs, then String, StringBuilder, StringBuffer What is the difference between the efficiency of these three people?

Talk is cheap, show me the code!

   
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;

 }

}

Very simple code, by adjusting the number of cycles, to print out three kinds of string connection time spent:


From the output above we can clearly see that in the number of cycles less (for example, less than 100), the time efficiency of the three, when with the increase in the number of cycles, the contrast effect has undergone a significant change.

We usually use strings to add directly to the connection, but it is best to use StringBuilder as a string connection, in fact, in the case of a small number of cycles we can also use string directly to add joins, but sometimes we simply can not determine the size of the cycle times, So it's best to be honest with StringBuilder.

Why is there so much performance difference between StringBuilder and String direct connection, let's analyze the following:

String str = "hello,world!";
String result = "";

for (int i = 0; i < Loopcount. i++) {result
= str;
}

The compiler will eventually compile the above code to resemble the following code:

String str = "hello,world!";
String result = "";

for (int i = 0; i < Loopcount i++) {result
= new StringBuilder (Result). Append (str). toString ();
}

When a new StringBuilder (result) is executed, the constructor StringBuilder copies all the characters in cause to the newly created StringBuilder. Each loop needs to create a StringBuilder object (it takes time and memory to create the object), and as the number of loops increases, the result string grows longer, and the longer it takes to copy the characters in result to the new StringBuilder, and StringBuilder (Result). Append (str). ToString () creates a temporary string, and as the number of cycles increases, the operation takes longer. In short, with the increase of the loop variable i, each cycle becomes more and more slow.

From the above output can be seen when the number of cycles increased, StringBuilder efficiency than stringbuffer better.

Here is a comparison between StringBuilder and StringBuffer:


When the number of cycles increases to 1000w, the difference is obvious, and the efficiency of StringBuilder is higher than that of StringBuffer.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.