The "Go" Java 5 string concatenation method performance comparison.

Source: Internet
Author: User

Recently wrote a dongdong, may consider the string splicing, think of several methods, but the performance is unknown, so with JUnit wrote 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 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 results of the operation are 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 ms
11:00:22,968 INFO teststring:58-stringbuffer Cost ms
11:00:23,031 INFO teststring:70-stringbuilder Cost + ms


Special attention is paid to the following:

The number of StringBuilder cycles is 10 times times that of the other, and if it is the same, then return 0, visible StringBuilder speed.

Summarize:

In the way of the worst efficiency, concat because it is the internal mechanism to achieve, more than the way of a lot better.

Join and StringBuffer, the difference is not small, join way faster, it can be seen in this JavaScript fast stitching string in the way in Java is also very suitable.

StringBuilder is the fastest, but its thread-safe problem, and only JDK5 support.

The "Go" Java 5 string concatenation method performance comparison.

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.