Test the relationship between stringbuilder, stringbuffer, and string classes.

Source: Internet
Author: User

To: gotesting software testing Alliance Forum http://www.78test.com

Public class testcharacter {

Final Static int time = 50000; // number of cycles

 

Public testcharacter (){



}

Public void test (string s ){

Long begin = system. currenttimemillis ();

For (INT I = 0; I <time; I ++ ){

S + = "add ";

}

Long over = system. currenttimemillis ();

System. Out. println ("operation" + S. getclass (). getname () + "type:" + (over-begin) + "millisecond ");

}

Public void test (stringbuffer s ){

Long begin = system. currenttimemillis ();

For (INT I = 0; I <time; I ++ ){

S. append ("add ");

}

Long over = system. currenttimemillis ();

System. Out. println ("operation" + S. getclass (). getcanonicalname () + "type:" + (over-begin) + "millisecond ");

}

Public void test (stringbuilder s ){

Long begin = system. currenttimemillis ();

For (INT I = 0; I <time; I ++ ){

S. append ("add ");

}

Long over = system. currenttimemillis ();

System. Out. println ("operation" + S. getclass (). getname () + "type:" + (over-begin) + "millisecond ");

}

/* Directly concatenate strings */

Public void Test2 (){

String S2 = "ABCD ";

Long begin = system. currenttimemillis ();

For (INT I = 0; I <time; I ++ ){

String S = S2 + S2 + S2;

}

Long over = system. currenttimemillis ();

System. Out. println ("Operation String object reference addition type used:" + (over-begin) + "millisecond ");

}

Public void test3 (){

Long begin = system. currenttimemillis ();

For (INT I = 0; I <time; I ++ ){

String S = "ABCD" + "ABCD" + "ABCD ";

}

Long over = system. currenttimemillis ();

System. Out. println ("the time for adding operation strings is:" + (over-begin) + "millisecond ");

}

Public static void main (string [] ARGs ){

String S1 = "ABCD ";

Stringbuffer ST1 = new stringbuffer ("ABCD ");

Stringbuilder st2 = new stringbuilder ("ABCD ");

Testcharacter Tc = new testcharacter ();

TC. Test (S1 );

TC. Test (ST1 );

TC. Test (st2 );

TC. Test2 ();

TC. test3 ();

}

} I have run this code in both myeclipse and DOS, and the printed time is somewhat different. The running result is as follows:

1) 10000 cycles in myeclipse:

2) 50000 cycles in myeclipse:

3) when running in DOS:

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.