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: