Multiple implementations and efficiency comparisons of Java string connections

Source: Internet
Author: User

Tag: Log object executes so target return modifies Java string mic

The new String.Join () method in JDK 1.8 (Java 8) is used for string connections. Based on the Java implementation of String.Join () and efficiency comparison, this paper analyzes and compares the efficiency of four custom implementations and String.Join () methods, and corrects some errors in the original text.

The code examples are as follows:

1  Public classTest {2      Public Static voidMain (string[] args) {3String[] Strori = {"A", "B", "C", "D", "E", "F", "G", "H"};//Same as new string[]{"A", "B", "C", "D", "E", "F", "G", "H"}4String strres = "";intLoops = 100000;5 6Date Date =NewDate ();7 8          for(inti = 0; i < loops; i++) {strres = Join1 (Strori, ":");} Date = Recordtime (date, 1);//19          for(inti = 0; i < loops; i++) {strres = Join2 (Strori, ":");} Date = Recordtime (date, 2);Ten          for(inti = 0; i < loops; i++) {strres = Join3 (Strori, ":");} Date = Recordtime (date, 3); One          for(inti = 0; i < loops; i++) {strres = Join4 (Strori, ":");} Date = Recordtime (date, 4); A          for(inti = 0; i < loops; i++) {strres = Join5 (Strori, ":");} Date = Recordtime (date, 5); -  -         LongStartTime = System.currenttimemillis ();//2 the          for(inti = 0; i < loops; i++) {strres = Join5 (Strori, ":");} -         LongEndTime = System.currenttimemillis (); System.out.println ("5c:{" + Strres + "} costs" + (Endtime-starttime) + "MS"); -  -StartTime =system.nanotime (); +          for(inti = 0; i < loops; i++) {strres = Join5 (Strori, ":");} -EndTime = System.nanotime (); System.out.println ("5n:{" + Strres + "} costs" + (Endtime-starttime) + "ns"); +     } A  at     Private Static voidRecordtime_wrong (date date,intNO) { -SYSTEM.OUT.PRINTLN (no + ": costs" + (NewDate (). GetTime ()-date.gettime ()) + "MS"); -Date =NewDate (); -     } -     Private StaticDate Recordtime (date date,intNO) { -SYSTEM.OUT.PRINTLN (no + ": costs" + (NewDate (). GetTime ()-date.gettime ()) + "MS"); in         return NewDate (); -     } to  +     Private Staticstring join1 (string[] Strori, String delimiter) { -StringBuffer SB =NewStringBuffer ();//3 the          for(String s:strori) { *Sb.append (S+delimiter);//4 $         }Panax Notoginseng         returnSb.tostring (). substring (0, sb.tostring (). Length ()-1); -     } the  +     Private Staticstring join2 (string[] Strori, String delimiter) { AStringBuffer SB =NewStringBuffer (); the          for(String s:strori) { +Sb.append (s+delimiter); -         } $String s =sb.tostring (); $         returnS.substring (0, S.length ()-1); -     } -  the     Private Staticstring join3 (string[] Strori, String delimiter) { -StringBuffer SB =NewStringBuffer ();Wuyi          for(inti = 0; i < strori.length; i++) { the             if(I! = strori.length-1) { -Sb.append (strori[i]+delimiter); Wu}Else { - sb.append (Strori[i]); About             } $         } -         returnsb.tostring (); -     } -  A     Private Staticstring Join4 (string[] Strori, String delimiter) { +StringBuilder StringBuilder =NewStringBuilder (); the          for(inti = 0; i < strori.length-1; i++) { - stringbuilder.append (Strori[i]). Append (delimiter); $         } theStringbuilder.append (strori[strori.length-1]); the         returnstringbuilder.tostring (); the     } the  -     Private Staticstring Join5 (string[] Strori, String delimiter) { in         returnString.Join (delimiter, Strori);//5 the     } the}

Select three run output results as follows:

1:costs 930ms2:costs 902ms3:costs 637ms4:costs 230ms5:costs 364ms5c:{a:b:c:d:e:f:g:h} costs 413ms5n:{a:b:c:d:e:f:g:h} Costs 286466296ns
1:costs 834ms2:costs 788ms3:costs 576ms4:costs 248ms5:costs 350ms5c:{a:b:c:d:e:f:g:h} costs 384ms5n:{a:b:c:d:e:f:g:h} Costs 283256112ns
1:costs 774ms2:costs 728ms3:costs 605ms4:costs 297ms5:costs 417ms5c:{a:b:c:d:e:f:g:h} costs 280ms5n:{a:b:c:d:e:f:g:h} Costs 279838638ns

Visible, JOIN4 () executes the fastest, followed by Join5 (). Join1 () and join2 () perform close, the former calls two ToString (), so the efficiency is slightly lower.

Summarized as follows:
1. In the original Recordtime (that is, this article Recordtime_wrong) method, the external date reference cannot be modified with "date = new Date ()" (the root is described in the Java value and pass-through reference). This causes each call to Recordtime () to have a starting time that is always the object obtained by date date = new Date (), which is represented as join* time-consuming increment.
2. To view the Java source, the new Date () is actually called System.currenttimemillis ():

1  Public Date () {2      This // equivalent to date (System.currenttimemillis ()) 3 }

You can use the new date (). GetTime () to get the current timestamp (in milliseconds). Note that the number of milliseconds is generally 1970-01-01 00:00:00 as the reference point, but the East eight zone to add the time zone, that is, the 1970-01-01 08:00:00 as the reference time. In addition, obtaining milliseconds through gettime () is less efficient than system.currenttimemillis (), which returns the number of milliseconds since 0 o'clock January 1, 1970.

The timing accuracy of system.nanotime () is not guaranteed to be higher than system.currenttimemillis (), but it is guaranteed to increment (the latter may produce negative values when subtracted).

For more accurate timing of your code, refer to the article "How does I write a correct micro-benchmark in Java?".
3. StringBuffer objects are thread-safe, and their methods are synchronous (synchronized). Temporary variables should use StringBuilder (more efficient) and avoid stringbuffer.
4. In the loop, do not use the form of append (a+b) and should be replaced by append (a). Append (b).
5. String.Join () is implemented internally using StringBuilder, so Join5 () performance is close to Join4 (). Of course, String.Join () has more features than JOIN4 ().

Multiple implementations and efficiency comparisons of Java string connections

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.