{"id":13531,"date":"2024-02-05T10:54:54","date_gmt":"2024-02-05T02:54:54","guid":{"rendered":"https:\/\/92it.top\/?p=13531"},"modified":"2024-02-05T10:56:33","modified_gmt":"2024-02-05T02:56:33","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-idea-%e5%bb%ba%e8%ae%ae%e5%8e%bb%e6%8e%89-stringbuilder%ef%bc%8c%e4%bd%bf%e7%94%a8%e6%8b%bc%e6%8e%a5%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=13531","title":{"rendered":"\u4e3a\u4ec0\u4e48 idea \u5efa\u8bae\u53bb\u6389 StringBuilder\uff0c\u4f7f\u7528\u201c+\u201d\u62fc\u63a5\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p>\u8f6c\u8f7d\uff1a<a href=\"https:\/\/mp.weixin.qq.com\/s\/-bzP0zgbCIbtvbD8B75swQ\">https:\/\/mp.weixin.qq.com\/s\/-bzP0zgbCIbtvbD8B75swQ<\/a><\/p>\n\n\n\n<p>\u5404\u4f4d\u5c0f\u4f19\u4f34\u5728\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\u5e94\u8be5\u90fd\u89c1\u8fc7\u4e0b\u9762\u8fd9\u79cd\u63d0\u793a\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-1024x112.png\" alt=\"\" class=\"wp-image-13532\" width=\"602\" height=\"65\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-1024x112.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-300x33.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-768x84.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-830x91.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-230x25.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-350x38.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11-480x53.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-11.png 1388w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure><\/div>\n\n\n\n<p>\u5927\u5bb6\u666e\u904d\u8ba4\u77e5\u4e2d\uff0c\u5b57\u7b26\u4e32\u62fc\u63a5\u8981\u7528 StringBuilder\uff0c\u90a3\u4e3a\u4ec0\u4e48 idea \u4f1a\u5efa\u8bae\u4f60\u662f\u7528\u201c+\u201d\u5462\uff0c\u90a3\u5230\u5e95 StringBuilder &nbsp;\u548c \u201c+\u201d\u6709\u4ec0\u4e48\u5177\u4f53\u533a\u522b\u5462\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u63a2\u7a76\u4e00\u4e0b\u3002<\/p>\n\n\n\n<p><strong>1\u3001\u666e\u901a\u62fc\u63a5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u666e\u901a\u7684\u51e0\u4e2a\u5b57\u7b26\u4e32\u62fc\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u76f4\u63a5\u4f7f\u7528\u201c+\u201d \u56e0\u4e3a\u6559\u6750\u7b49\u539f\u56e0\uff0c\u5f53\u524d\u4f9d\u65e7\u6709\u8bb8\u591a\u4eba\u62fc\u63a5\u5b57\u7b26\u4e32\u65f6\u8ba4\u4e3a\u4f7f\u7528\u201c+\u201d\u8017\u6027\u80fd\uff0c\u9996\u9009StringBuilder\u3002<br>\u5b9e\u9645\u4e0a\uff0c\u4eceJDK5\u5f00\u59cb\uff0cJava\u7f16\u8bd1\u5668\u5c31\u505a\u4e86\u4f18\u5316\uff0c\u4f7f\u7528\u201c+\u201d\u62fc\u63a5\u5b57\u7b26\u4e32\uff0c\u7f16\u8bd1\u5668\u7f16\u8bd1\u540e\u5b9e\u9645\u5c31\u81ea\u52a8\u4f18\u5316\u4e3a\u4f7f\u7528StringBuilder\u3002<br>\u65b0\u5efa\u6d4b\u8bd5\u7c7bStringTest\uff0c\u5206\u522b\u521b\u5efa\u4f7f\u7528\u201c+\u201d\u62fc\u63a5\u5b57\u7b26\u4e32\u548c\u4f7f\u7528StringBuilder\u62fc\u63a5\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\uff1b\u5e76\u65b0\u589eJunit\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5206\u522b\u8c03\u7528\u62fc\u63a5\u5b57\u7b26\u4e32100000\u6b21\uff08\u8fd9\u91cc\u4e0d\u662f\u5faa\u73af\u62fc\u63a5\uff0c\u800c\u662f\u6267\u884c\u591a\u6b21\u62fc\u63a5\uff0c\u56e0\u4e3a\u4e00\u6b21\u62fc\u63a5\u8017\u65f6\u592a\u5c11\uff0c\u770b\u4e0d\u51fa\u5dee\u5f02\uff09\uff0c\u6253\u5370\u8017\u65f6\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * \u4f7f\u7528+\u62fc\u63a5\u5b57\u7b26\u4e32\n *\/\npublic String concatenationStringByPlus(String prefix, int i) {\n    return prefix + \"-\" + i;\n}\n\n\/**\n * \u4f7f\u7528StringBuilder\u62fc\u63a5\u5b57\u7b26\u4e32\n *\/\npublic String concatenationStringByStringBuilder(String prefix, int i) {\n    return new StringBuilder().append(prefix).append(\"-\").append(i).toString();\n}\n\n\/**\n * \u6d4b\u8bd5\u4f7f\u7528+\u62fc\u63a5\u5b57\u7b26\u4e32\u8017\u65f6\n *\/\n@Test\npublic void testStringConcatenation01ByPlus() {\n    long startTime = System.currentTimeMillis();\n    int count = 100000;\n    for (int i = 0; i &lt; count; i++) {\n        String str = concatenationStringByPlus(\"testStringConcatenation01ByStringBuilder:\", i);\n    }\n    long endTime = System.currentTimeMillis();\n    System.out.println(\"testStringConcatenation01ByPlus\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\" + count + \"\u6b21\uff0c\u82b1\u8d39\" + (endTime - startTime) + \"\u79d2\");\n}\n\n\n\/**\n * \u6d4b\u8bd5\u4f7f\u7528StringBuilder\u62fc\u63a5\u5b57\u7b26\u4e32\u8017\u65f6\n *\/\n@Test\npublic void testStringConcatenation02ByStringBuilder() {\n    long startTime = System.currentTimeMillis();\n    int count = 100000;\n    for (int i = 0; i &lt; count; i++) {\n        String str = concatenationStringByStringBuilder(\"testStringConcatenation02ByStringBuilder:\", i);\n    }\n    long endTime = System.currentTimeMillis();\n    System.out.println(\"testStringConcatenation02ByStringBuilder\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\" + count + \"\u6b21\uff0c\u82b1\u8d39\" + (endTime - startTime) + \"\u79d2\");\n}<\/pre>\n\n\n\n<p>\u6267\u884cJunit\u7528\u4f8b\uff0c\u770b\u8017\u65f6\u7edf\u8ba1\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">testStringConcatenation01ByPlus\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32100000\u6b21\uff0c\u82b1\u8d3933\u79d2\ntestStringConcatenation02ByStringBuilder\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32100000\u6b21\uff0c\u82b1\u8d3936\u79d2<\/pre>\n\n\n\n<p>\u867d\u7136\u6709\u5dee\u5f02\uff0c\u4f46\u662f\u5dee\u5f02\u6781\u5c0f\uff0c\u8003\u8651\u5230\u6267\u884c\u4e86100000\u6b21\uff0c\u6bcf\u6b21\u8017\u65f6\u7684\u5dee\u5f02\u5c31\u66f4\u5c0f\u4e86\uff0c\u800c\u4e14\u7a0b\u5e8f\u6267\u884c\u6709\u5404\u79cd\u56e0\u7d20\u5f71\u54cd\u6267\u884c\u6548\u7387\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u8017\u65f6\u5dee\u4e0d\u591a\u3002\u4e5f\u53ef\u4ee5\u591a\u6b21\u6267\u884c\u5bf9\u6bd4\u8017\u65f6\u5dee\u5f02\uff0c\u4e5f\u53ef\u4ee5\u53d1\u73b0\u57fa\u672c\u4e00\u81f4\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n\n\n\n<p>\u5230class\u6587\u4ef6\u6240\u5728\u76ee\u5f55\uff0c\u6267\u884c javap -c StringTest.class\uff0c\u5bf9class\u6587\u4ef6\u8fdb\u884c\u53cd\u7f16\u8bd1\uff0c\u67e5\u770b\u7f16\u8bd1\u540e\u7684\u4ee3\u7801\u5dee\u5f02\u3002\u8fd9\u91cc\u4e0d\u8981\u4f7f\u7528Intellij idea\u548cJD\u8fdb\u884c\u53cd\u7f16\u8bd1\uff0c\u56e0\u4e3a\u53cd\u7f16\u8bd1\u6709\u4f18\u5316\uff0c\u4f1a\u90fd\u53cd\u7f16\u8bd1\u6210\u201c+\u201d\u62fc\u63a5\u7684\uff0c\u770b\u4e0d\u51fa\u6765\u7f16\u8bd1\u540e\u7684\u771f\u6b63\u60c5\u51b5\u3002<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-1024x444.png\" alt=\"\" class=\"wp-image-13533\" width=\"467\" height=\"202\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-1024x444.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-300x130.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-768x333.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-830x360.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-230x100.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-350x152.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12-480x208.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2024\/02\/image-12.png 1374w\" sizes=\"(max-width: 467px) 100vw, 467px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u56fe\u4e0a\u53ef\u4ee5\u770b\u51fa\u4e24\u79cd\u62fc\u63a5\u65b9\u6cd5\u53cd\u7f16\u8bd1\u540e\u5b8c\u5168\u4e00\u6837\uff0c\u6ca1\u6709\u5dee\u5f02\uff0c\u6267\u884c\u6548\u7387\u81ea\u7136\u4e5f\u662f\u4e00\u6837\u7684\u3002<br>\u65e2\u7136\u6267\u884c\u6548\u7387\u4e00\u6837\uff0c\u4ece\u4ee3\u7801\u7b80\u6d01\u5229\u4e8e\u9605\u8bfb\u8003\u8651\uff0c\u63a8\u8350\u4f7f\u7528\u201c+\u201d\u62fc\u63a5\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p><strong>2\u3001\u5faa\u73af\u62fc\u63a5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5faa\u73af\u62fc\u63a5\u4e00\u4e2a\u957f\u5b57\u7b26\u4e32\uff0c\u5efa\u8bae\u4f7f\u7528StringBuilder\uff0c\u867d\u7136\u201c+\u201d\u62fc\u63a5\u5b57\u7b26\u4e32\u7f16\u8bd1\u540e\u4e5f\u4f1a\u53d8\u6210StringBuilder\uff0c\u4f46\u662f\u6bcf\u6b21\u5faa\u73af\u5904\u7406\u90fd\u4f1anew\u4e00\u4e2aStringBuilder\u5bf9\u8c61\uff0c\u8017\u65f6\u4f1a\u5927\u5927\u589e\u52a0\u3002\u800c\u76f4\u63a5\u4f7f\u7528StringBuilder\uff0cnew\u4e00\u6b21\u5c31\u53ef\u4ee5\u4e86\uff0c\u6548\u7387\u76f8\u5bf9\u9ad8\u3002<br>\u65b0\u589e2\u4e2aJunit\u6d4b\u8bd5\u7528\u4f8b\uff0c\u5faa\u73af\u62fc\u63a510000\u6b21\u62fc\u63a5\u4e00\u4e2a\u5b57\u7b26\u4e32\uff08\u6b21\u6570\u5c11\u4e8e\u4e0a\u9762\u7684\u7528\u4f8b\uff0c\u56e0\u4e3a\u62fc\u63a5\u7684\u662f\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u62fc\u63a5\u6b21\u6570\u592a\u591a\uff0c\u53ef\u80fd\u5f15\u53d1\u5185\u5b58\u6ea2\u51fa\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/**\n * \u5faa\u73af\u4f7f\u7528+\u62fc\u63a5\u5b57\u7b26\u4e32\n *\/\n@Test\npublic void testLoopStringConcatenation03ByPlus() {\n    long startTime = System.currentTimeMillis();\n    int count = 10000;\n    String str = \"testLoopStringConcatenation03ByPlus:\";\n    for (int i = 0; i &lt; count; i++) {\n        str = str + \"-\" + i;\n    }\n    System.out.println(str);\n    long endTime = System.currentTimeMillis();\n    System.out.println(\"testLoopStringConcatenation03ByPlus\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\" + count + \"\u6b21\uff0c\u82b1\u8d39\" + (endTime - startTime) + \"\u79d2\");\n}\n\n\/**\n * \u6d4b\u8bd5\u5faa\u73af\u4f7f\u7528StringBuilder\u62fc\u63a5\u5b57\u7b26\u4e32\u8017\u65f6\n *\/\n@Test\npublic void testLoopStringConcatenation04ByStringBuilder() {\n    long startTime = System.currentTimeMillis();\n    int count = 100000;\n    StringBuilder stringBuilder = new StringBuilder(\"testLoopStringConcatenation04ByStringBuilder:\");\n    for (int i = 0; i &lt; count; i++) {\n        stringBuilder.append(\"-\");\n        stringBuilder.append(i);\n    }\n    String str = stringBuilder.toString();\n    System.out.println(str);\n    long endTime = System.currentTimeMillis();\n    System.out.println(\"testLoopStringConcatenation04ByStringBuilder\uff0c\u62fc\u63a5\u5b57\u7b26\u4e32\" + count + \"\u6b21\uff0c\u82b1\u8d39\" + (endTime - startTime) + \"\u79d2\");\n}<\/pre>\n\n\n\n<p>\u6267\u884cJunit\u7528\u4f8b\uff0c\u770b\u8017\u65f6\u7edf\u8ba1\u8f93\u51fa\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">testLoopStringConcatenation03ByPlus\uff0c\u62fc\u63a5\u5b57\u7b26\u4e3210000\u6b21\uff0c\u82b1\u8d39463\u79d2\ntestLoopStringConcatenation04ByStringBuilder\uff0c\u62fc\u63a5\u5b57\u7b26\u4e3210000\u6b21\uff0c\u82b1\u8d3913\u79d2<\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u51fa\uff0c\u5dee\u5f02\u660e\u663e\uff0c\u4e0d\u5728\u4e00\u4e2a\u91cf\u7ea7\u4e86\u3002<\/p>\n\n\n\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<ul><li>\u5355\u7eaf\u7684\u5b57\u7b26\u4e32\u62fc\u63a5\u4f7f\u7528\u201c+\u201d\uff0c\u66f4\u5feb\u66f4\u7b80\u6d01\u3002<\/li><li>\u5faa\u73af\u62fc\u63a5\u65f6\u4f7f\u7528\u201c+\u201d\u62fc\u63a5\u5b57\u7b26\u4e32\u6548\u7387\u8f83\u4f4e\uff0c\u63a8\u8350\u4f7f\u7528 StringBuilder\u3002<\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u8f6c\u8f7d\uff1ahttps:\/\/mp.weixin.qq.com\/s\/-bzP0zgbCIbtvbD8B75swQ \u5404\u4f4d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/13531"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=13531"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/13531\/revisions"}],"predecessor-version":[{"id":13535,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/13531\/revisions\/13535"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=13531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=13531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=13531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}