Java 5种字符串拼接方式性能比较 博客分类: java java
程序员文章站
2024-03-26 12:15:11
...
最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试。
代码如下:
1.import java.util.ArrayList;
2.import java.util.List;
3.
4.import org.apache.commons.lang.StringUtils;
5.import org.junit.Test;
6.import org.slf4j.Logger;
7.import org.slf4j.LoggerFactory;
8.
9.public class TestString {
10.
11. private final Logger logger = LoggerFactory.getLogger(this.getClass());
12.
13. @Test
14. public void testPlus() {
15. String s = "";
16. long ts = System.currentTimeMillis();
17. for (int i = 0; i < 10000; i++) {
18. s = s + String.valueOf(i);
19. }
20. long te = System.currentTimeMillis();
21. logger.info("+ cost {} ms", te - ts);
22. }
23.
24. @Test
25. public void testConcat() {
26. String s = "";
27. long ts = System.currentTimeMillis();
28. for (int i = 0; i < 10000; i++) {
29. s = s.concat(String.valueOf(i));
30. }
31. long te = System.currentTimeMillis();
32. logger.info("concat cost {} ms", te - ts);
33. }
34.
35. @Test
36. public void testJoin() {
37. List<String> list = new ArrayList<String>();
38. long ts = System.currentTimeMillis();
39. for (int i = 0; i < 10000; i++) {
40. list.add(String.valueOf(i));
41. }
42. StringUtils.join(list, "");
43. long te = System.currentTimeMillis();
44. logger.info("StringUtils.join cost {} ms", te - ts);
45. }
46.
47. @Test
48. public void testStringBuffer() {
49. StringBuffer sb = new StringBuffer();
50. long ts = System.currentTimeMillis();
51. for (int i = 0; i < 10000; i++) {
52. sb.append(String.valueOf(i));
53. }
54. sb.toString();
55. long te = System.currentTimeMillis();
56. logger.info("StringBuffer cost {} ms", te - ts);
57. }
58.
59. @Test
60. public void testStringBuilder() {
61. StringBuilder sb = new StringBuilder();
62. long ts = System.currentTimeMillis();
63. for (int i = 0; i < 100000; i++) {
64. sb.append(String.valueOf(i));
65. }
66. sb.toString();
67. long te = System.currentTimeMillis();
68. logger.info("StringBuilder cost {} ms", te - ts);
69. }
70.}
71.
运行结果如下:
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
要特别注意的是:
StringBuilder 循环的次数是其它的10倍,如果是一样,那么返回 0,可见StringBuilder 的速度之快。
总结:
用+的方式效率最差,concat由于是内部机制实现,比+的方式好了不少。
Join 和 StringBuffer,相差不大,Join方式要快些,可见这种JavaScript中快速拼接字符串的方式在Java中也非常适用。
StringBuilder 的速度最快,但其有线程安全的问题,而且只有JDK5支持。
原文:http://blog.csdn.net/kimsoft/article/details/3353849
代码如下:
1.import java.util.ArrayList;
2.import java.util.List;
3.
4.import org.apache.commons.lang.StringUtils;
5.import org.junit.Test;
6.import org.slf4j.Logger;
7.import org.slf4j.LoggerFactory;
8.
9.public class TestString {
10.
11. private final Logger logger = LoggerFactory.getLogger(this.getClass());
12.
13. @Test
14. public void testPlus() {
15. String s = "";
16. long ts = System.currentTimeMillis();
17. for (int i = 0; i < 10000; i++) {
18. s = s + String.valueOf(i);
19. }
20. long te = System.currentTimeMillis();
21. logger.info("+ cost {} ms", te - ts);
22. }
23.
24. @Test
25. public void testConcat() {
26. String s = "";
27. long ts = System.currentTimeMillis();
28. for (int i = 0; i < 10000; i++) {
29. s = s.concat(String.valueOf(i));
30. }
31. long te = System.currentTimeMillis();
32. logger.info("concat cost {} ms", te - ts);
33. }
34.
35. @Test
36. public void testJoin() {
37. List<String> list = new ArrayList<String>();
38. long ts = System.currentTimeMillis();
39. for (int i = 0; i < 10000; i++) {
40. list.add(String.valueOf(i));
41. }
42. StringUtils.join(list, "");
43. long te = System.currentTimeMillis();
44. logger.info("StringUtils.join cost {} ms", te - ts);
45. }
46.
47. @Test
48. public void testStringBuffer() {
49. StringBuffer sb = new StringBuffer();
50. long ts = System.currentTimeMillis();
51. for (int i = 0; i < 10000; i++) {
52. sb.append(String.valueOf(i));
53. }
54. sb.toString();
55. long te = System.currentTimeMillis();
56. logger.info("StringBuffer cost {} ms", te - ts);
57. }
58.
59. @Test
60. public void testStringBuilder() {
61. StringBuilder sb = new StringBuilder();
62. long ts = System.currentTimeMillis();
63. for (int i = 0; i < 100000; i++) {
64. sb.append(String.valueOf(i));
65. }
66. sb.toString();
67. long te = System.currentTimeMillis();
68. logger.info("StringBuilder cost {} ms", te - ts);
69. }
70.}
71.
运行结果如下:
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
要特别注意的是:
StringBuilder 循环的次数是其它的10倍,如果是一样,那么返回 0,可见StringBuilder 的速度之快。
总结:
用+的方式效率最差,concat由于是内部机制实现,比+的方式好了不少。
Join 和 StringBuffer,相差不大,Join方式要快些,可见这种JavaScript中快速拼接字符串的方式在Java中也非常适用。
StringBuilder 的速度最快,但其有线程安全的问题,而且只有JDK5支持。
原文:http://blog.csdn.net/kimsoft/article/details/3353849