Java connection string in several ways

Source: Internet
Author: User

Public class stringtest implements clock {    private int  i = 0;    public void teststring ()  {         string str = new string ();         int j = i;        for (; i  < j + 40000; i++)  {             str += string.valueof (i);        }     }    public void teststringbuffer ()  {         stringbuffer sb = new stringbuffer ();         int j = i;        for (;  i  < j + 40000; i++)  {            sb.append ( String.valueof (i));        }    }     public void teststringbuilder ()  {         Stringbuilder sb = new stringbuilder ();         int  j = i;        for (;  i < j +  40000; i++)  {            sb.append (String.valueof (i));        }    }     public static void main (String[] args)  {         stringtest test = new stringtest ();         test.stamp ("String",  test::teststring);         test.stamp ("StringBuffer",  test:: Teststringbuffer);         test.stamp ("StringBuilder",  test:: Teststringbuilder);         test.stamp ("String",  test::testString) ;         test.stamp ("StringBuffer",  test::teststringbuffer);         test.stamp ("StringBuilder",  test::teststringbuilder);         test.stamp ("String",  test::teststring);         test.stamp ("StringBuffer",  test::teststringbuffer);         test.stamp ("StringBuilder",  test::teststringbuilder);     }}

Briefly explain the test:

    1. Environment, Mac OS 10.10.4, CPU 2.6 GHz Intel Core i5, Memory 8G;

    2. Each test ran 40,000 times, timed, and recorded the results;

    3. Run three times;

The result is:

It takes [110000000] nono seconds to process [string]it takes [5000000] nono seconds to process [Stringbuffer]it takes [50  00000] Nono seconds to process [stringbuilder]it takes [654000000] nono seconds to process [string]it takes [4000000] Nono Seconds to process [stringbuffer]it takes [3000000] nono seconds to process [stringbuilder]it takes [565000000] Nono Seco  NDS to process [String]it takes [3000000] nono seconds to process [stringbuffer]it takes [3000000] nono seconds to process [StringBuilder]

As can be seen from this result,

    1. As expected, the latter two are better than the first, 15 times times 20 times times the same way;

    2. The latter two methods are more stable than the first one, and they are probably really fast, yet not to the unstable stage;

    3. StringBuilder is very close to stringbuffer performance, slightly better.


The reason why StringBuilder and StringBuffer are tested here is because I remember the recommendation to use StringBuilder if not involved in concurrency, presumably because StringBuilder has no synchronization control;

Look at the source of StringBuilder and StringBuffer, it is true:

StringBuilder Append code:

@Overridepublic StringBuilder Append (String str) {super.append (str); return this;}


StringBuffer Append code:

@Overridepublic synchronized stringbuffer append (String str) {tostringcache = null;    Super.append (str); return this;}


But in a method where there is no concurrency control, Java should be optimized to remove this lock. (Unfortunately I do not know how to test such a situation), so in most cases, the use of StringBuffer will not become a problem;

Java connection string in several ways

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.