The efficiency of several Java concatenation strings

Source: Internet
Author: User

Stitching strings, roughly 3 classes can be used, they are string, Stringbuffer,stringbuilder, StringBuilder is 1.5 in place of StringBuffer. The test method is as follows:

public class Test {/** * @param args */public static void main (string[] args) {//TODO auto-generated Metho 
        D stub long n = 30000; System.out.println ("Start ...")   
           
        "+n);    
        Long Start1 = System.currenttimemillis ();    
        string S1 = new String ("Hello");    
        for (long i = 0; i < n; i++) {s1+= "The time of the concatenation string";    
        Long end1 = System.currenttimemillis ();    
        Long time1 = End1-start1;    
              
        System.out.println ("Time of concatenation of strings with string+=" +time1);    
        Long Start2 = System.currenttimemillis ();    
        String s2 = new string ("Hello");    
        for (long i = 0; i < n; i++) {s2=s2+ "The time of the concatenation string";    
        Long End2 = System.currenttimemillis ();    
        Long time2 = End2-start2;  
           
        System.out.println ("Time of concatenation of strings with string=string+" +time2); Long Start3 = System.currenttimemillis ();    
        String s3 = new String ("Hello");    
        for (long i = 0; i < n; i++) {S3=s3.concat ("Time of concatenation string");    
        Long End3 = System.currenttimemillis ();    
        Long Time3 = END3-START3;  
           
        System.out.println ("Time of concatenation of strings with String.Concat" +time3);    
        Long start4 = System.currenttimemillis ();    
        StringBuffer S4 = new StringBuffer ("Hello");    
        for (long i = 0; i < n; i++) {S4.append ("Time of concatenation string");    
        Long End4 = System.currenttimemillis ();    
        Long time4 = END4-START4;    
              
        System.out.println ("Time of concatenation of strings with Stringbuffer.append" +time4);    
        Long start5 = System.currenttimemillis ();    
        StringBuilder S5 = new StringBuilder ("Hello");    
        for (long i = 0; i < n; i++) {S5.append ("Time of concatenation string"); Long End5 = System.currenttimemillis ();    
        Long time5 = End5-start5;    
           
        System.out.println ("Time of concatenation of strings with Stringbuilder.append" +time5);   
System.out.println ("End ..."); A set of test data is posted as follows:1234567start... 
30000 time of stitching string with string+= 27468 time of concatenation string with string=string+ 25813 time of concatenation string with String.Concat 12265  Concatenation of strings with Stringbuffer.append time 14 with stringbuilder.append concatenation string time 8 end ...

1, the String class itself is final type, string concatenation, will use StringBuffer, and call append, and then call the ToString method. While StringBuffer is converted to a string, the overhead is quite large. The middle has not only created a temporary object StringBuffer, but also to turn to string after each time.

2, in doing string concatenation, the String class Concat method is superior to the + number. (String = =) is equivalent to the (string = string +) phase rate.


3, and the appearance of StringBuilder is used to replace stringbuffer, but not suitable for multithreaded programming. From this point of view, StringBuilder in the case of single-threaded programming should take precedence over the use of StringBuffer, while multithreaded programming should use StringBuffer, not to use StringBuilder.

4, the single threaded case to do string concatenation, stringbuilder.append and stringbuffer.append efficiency is equivalent. Reprint Link: http://blog.csdn.net/amork/article/details/4307072

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.