Efficiency of several concatenated strings. Efficiency of several concatenated strings
- Public class test {
- /**
- * @ Param args
- */
- Public static void main (String [] args ){
- // TODO Auto-generated method 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 + = "time of concatenating strings ";
- }
- Long end1 = System. currentTimeMillis ();
- Long time1 = end1-start1;
- System. out. println ("time of concatenating strings with String + =" + time1 );
- Long start2 = System. currentTimeMillis ();
- String s2 = new String ("hello ");
- For (long I = 0; I <n; I ++)
- {
- S2 = s2 + "time of String concatenation ";
- }
- Long end2 = System. currentTimeMillis ();
- Long time2 = end2-start2;
- System. out. println ("String = String + time to splice strings" + time2 );
- Long start3 = System. currentTimeMillis ();
- String s3 = new String ("hello ");
- For (long I = 0; I <n; I ++)
- {
- S3 = s3.concat ("concatenation string time ");
- }
- Long end3 = System. currentTimeMillis ();
- Long time3 = end3-start3;
- System. out. println ("Time to concatenate a String with String. concat" + time3 );
- Long start4 = System. currentTimeMillis ();
- StringBuffer s4 = new StringBuffer ("hello ");
- For (long I = 0; I <n; I ++)
- {
- S4.append ("time of String concatenation ");
- }
- Long end4 = System. currentTimeMillis ();
- Long time4 = end4-start4;
- System. out. println ("Time for concatenating 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 String concatenation ");
- }
- Long end5 = System. currentTimeMillis ();
- Long time5 = end5-start5;
- System. out. println ("Time for concatenating strings with StringBuilder. append" + time5 );
- System. out. println ("End ...");
- }
- }
Paste a set of detection data as follows:
[Java]View plaincopy
- The time for concatenating strings with String + = 27468
- The time for concatenating strings with String = String + is 25813.
- The time for concatenating strings with String. concat is 12265.
- String concatenation time with StringBuffer. append 14
- Time for concatenating strings with StringBuilder. append 8