Efficiency of several concatenated strings. Efficiency of several concatenated strings

Source: Internet
Author: User

Efficiency of several concatenated strings. Efficiency of several concatenated strings

  1. Public class test {
  2. /**
  3. * @ Param args
  4. */
  5. Public static void main (String [] args ){
  6. // TODO Auto-generated method stub
  7. Long n = 30000;
  8. System. out. println ("Start..." + n );
  9. Long start1 = System. currentTimeMillis ();
  10. String s1 = new String ("hello ");
  11. For (long I = 0; I <n; I ++)
  12. {
  13. S1 + = "time of concatenating strings ";
  14. }
  15. Long end1 = System. currentTimeMillis ();
  16. Long time1 = end1-start1;
  17. System. out. println ("time of concatenating strings with String + =" + time1 );
  18. Long start2 = System. currentTimeMillis ();
  19. String s2 = new String ("hello ");
  20. For (long I = 0; I <n; I ++)
  21. {
  22. S2 = s2 + "time of String concatenation ";
  23. }
  24. Long end2 = System. currentTimeMillis ();
  25. Long time2 = end2-start2;
  26. System. out. println ("String = String + time to splice strings" + time2 );
  27. Long start3 = System. currentTimeMillis ();
  28. String s3 = new String ("hello ");
  29. For (long I = 0; I <n; I ++)
  30. {
  31. S3 = s3.concat ("concatenation string time ");
  32. }
  33. Long end3 = System. currentTimeMillis ();
  34. Long time3 = end3-start3;
  35. System. out. println ("Time to concatenate a String with String. concat" + time3 );
  36. Long start4 = System. currentTimeMillis ();
  37. StringBuffer s4 = new StringBuffer ("hello ");
  38. For (long I = 0; I <n; I ++)
  39. {
  40. S4.append ("time of String concatenation ");
  41. }
  42. Long end4 = System. currentTimeMillis ();
  43. Long time4 = end4-start4;
  44. System. out. println ("Time for concatenating strings with StringBuffer. append" + time4 );
  45. Long start5 = System. currentTimeMillis ();
  46. StringBuilder s5 = new StringBuilder ("hello ");
  47. For (long I = 0; I <n; I ++)
  48. {
  49. S5.append ("time of String concatenation ");
  50. }
  51. Long end5 = System. currentTimeMillis ();
  52. Long time5 = end5-start5;
  53. System. out. println ("Time for concatenating strings with StringBuilder. append" + time5 );
  54. System. out. println ("End ...");
  55. }
  56. }



 

Paste a set of detection data as follows:

 

[Java]View plaincopy
  1. The time for concatenating strings with String + = 27468
  2. The time for concatenating strings with String = String + is 25813.
  3. The time for concatenating strings with String. concat is 12265.
  4. String concatenation time with StringBuffer. append 14
  5. Time for concatenating strings with StringBuilder. append 8

Related Article

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.