Performance Comparison of Java five String concatenation methods.

Source: Internet
Author: User

Recently I wrote a plug-in that may consider String concatenation. I thought about several methods, but the performance was unknown. So I used JUnit to write a unit test.

 

The Code is as follows:

  1. Import java. util. arraylist;
  2. Import java. util. List;
  3. Import org. Apache. commons. Lang. stringutils;
  4. Import org. JUnit. test;
  5. Import org. slf4j. Logger;
  6. Import org. slf4j. loggerfactory;
  7. Public class teststring {
  8. Private Final logger = loggerfactory. getlogger (this. getclass ());
  9. @ Test
  10. Public void testplus (){
  11. String S = "";
  12. Long Ts = system. currenttimemillis ();
  13. For (INT I = 0; I <10000; I ++ ){
  14. S = S + String. valueof (I );
  15. }
  16. Long TE = system. currenttimemillis ();
  17. Logger.info ("+ cost {} ms", te-ts );
  18. }
  19. @ Test
  20. Public void testconcat (){
  21. String S = "";
  22. Long Ts = system. currenttimemillis ();
  23. For (INT I = 0; I <10000; I ++ ){
  24. S = S. Concat (string. valueof (I ));
  25. }
  26. Long TE = system. currenttimemillis ();
  27. Logger.info ("Concat cost {} ms", te-ts );
  28. }
  29. @ Test
  30. Public void testjoin (){
  31. List <string> List = new arraylist <string> ();
  32. Long Ts = system. currenttimemillis ();
  33. For (INT I = 0; I <10000; I ++ ){
  34. List. Add (string. valueof (I ));
  35. }
  36. Stringutils. Join (list ,"");
  37. Long TE = system. currenttimemillis ();
  38. Logger.info ("stringutils. Join cost {} ms", te-ts );
  39. }
  40. @ Test
  41. Public void teststringbuffer (){
  42. Stringbuffer sb = new stringbuffer ();
  43. Long Ts = system. currenttimemillis ();
  44. For (INT I = 0; I <10000; I ++ ){
  45. SB. append (string. valueof (I ));
  46. }
  47. SB. tostring ();
  48. Long TE = system. currenttimemillis ();
  49. Logger.info ("stringbuffer cost {} ms", te-ts );
  50. }
  51. @ Test
  52. Public void teststringbuilder (){
  53. Stringbuilder sb = new stringbuilder ();
  54. Long Ts = system. currenttimemillis ();
  55. For (INT I = 0; I <100000; I ++ ){
  56. SB. append (string. valueof (I ));
  57. }
  58. SB. tostring ();
  59. Long TE = system. currenttimemillis ();
  60. Logger.info ("stringbuilder cost {} ms", te-ts );
  61. }
  62. }

 

The running result is as follows:

11:00:22, 359 info teststring: 23-+ cost 1828 MS
11:00:22, 921 info teststring: 34-Concat cost 562 MS
11:00:22, 937 info teststring: 46-stringutils. Join cost 16 MS
11:00:22, 968 info teststring: 58-stringbuffer cost 31 MS
11:00:23, 031 info teststring: 70-stringbuilder cost 63 MS

 

Note:

The number of stringbuilder loops is 10 times the number of other loops. If the number is the same, 0 is returned, which shows that stringbuilder is faster.

 

Summary:

The efficiency of using + is the worst. Because Concat is implemented by internal mechanisms, it is much better than the + method.

Join and stringbuffer are not much different. The join method is faster. It can be seen that this method of rapid String concatenation in Javascript is also very suitable in Java.

Stringbuilder has the fastest speed, but it has thread security issues and is only supported by jdk5.

 

 

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.