{"id":207012,"date":"2025-07-08T08:35:04","date_gmt":"2025-07-08T00:35:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207012\/"},"modified":"2025-07-08T08:35:04","modified_gmt":"2025-07-08T00:35:04","slug":"%e5%a2%9e%e5%8a%a0%e5%9c%b0%e5%9b%be%e6%9c%80%e5%bf%ab%e7%9a%84%e6%96%b9%e6%b3%95%e6%98%af%e4%bb%80%e4%b9%88%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207012\/","title":{"rendered":"\u589e\u52a0\u5730\u56fe\u6700\u5feb\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u589e\u52a0\u5730\u56fe\u6700\u5feb\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-19 14:45:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u6211\u4eec\u8981\u52aa\u529b\u5b66\u4e60\u5566\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u589e\u52a0\u5730\u56fe\u6700\u5feb\u7684\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u300b<\/span>\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u8bf7\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427\uff01\u4e0b\u6587\u4e2d\u7684\u5185\u5bb9\u6211\u4eec\u4e3b\u8981\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u5728\u9605\u8bfb\u672c\u6587\u8fc7\u7a0b\u4e2d\u6709\u9047\u5230\u4e0d\u6e05\u695a\u7684\u5730\u65b9\uff0c\u6b22\u8fce\u7559\u8a00\u5440\uff01\u6211\u4eec\u4e00\u8d77\u8ba8\u8bba\uff0c\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6ce8\u610f\u5230 <code>map[int]int<\/code> \u53d8\u91cf\u7684\u4ee5\u4e0b\u4e24\u79cd\u589e\u91cf\u65b9\u6cd5\u7684\u901f\u5ea6\u7cfb\u6570\u4e3a 3 \u500d\uff1a<\/p>\n<p>\u5feb\u901f\uff1a<code>mymap[key]++<\/code><\/p>\n<p>\u6162\uff1a<code>mymap[key]=mymap[key]+1<\/code><\/p>\n<p>\u8fd9\u53ef\u80fd\u5e76\u4e0d\u5947\u602a\uff0c\u56e0\u4e3a\u81f3\u5c11\u5929\u771f\u5730\uff0c\u5728\u7b2c\u4e8c\u79cd\u60c5\u51b5\u4e0b\uff0c\u6211\u6307\u793a go \u8bbf\u95ee mymap \u4e24\u6b21\u3002\u6211\u53ea\u662f\u597d\u5947\uff1a\u4efb\u4f55\u719f\u6089 go \u7f16\u8bd1\u5668\u7684\u4eba\u90fd\u53ef\u4ee5\u5e2e\u52a9\u6211\u7406\u89e3\u5730\u56fe\u4e0a\u8fd9\u4e9b\u64cd\u4f5c\u4e4b\u95f4\u7684\u533a\u522b\u5417\uff1f\u4e86\u89e3\u7f16\u8bd1\u5668\u7684\u5de5\u4f5c\u539f\u7406\u540e\uff0c\u662f\u5426\u6709\u66f4\u5feb\u7684\u6280\u5de7\u6765\u589e\u52a0\u6620\u5c04\uff1f<\/p>\n<p>\u7f16\u8f91\uff1a\u5728\u672c\u5730\u8fd0\u884c\u65f6\u5dee\u5f02\u4e0d\u592a\u660e\u663e\uff0c\u4f46\u4ecd\u7136\u5b58\u5728\uff1a<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"math\"\n    \"time\"\n)\n\nfunc main() {\n\n    x, y := make(map[int]int), make(map[int]int)\n    x[0], y[0] = 0, 0\n    steps := int(math.pow(10, 9))\n\n    start1 := time.now()\n    for i := 0; i &lt; steps; i++ {\n        x[0]++\n    }\n    elapsed1 := time.since(start1)\n    fmt.println(\"++ took\", elapsed1)\n\n    start2 := time.now()\n    for i := 0; i &lt; steps; i++ {\n        y[0] = y[0] + 1\n    }\n    elapsed2 := time.since(start2)\n\n    fmt.println(\"y=y+1 took\", elapsed2)\n\n}<\/pre>\n<p>\u8f93\u51fa\uff1a<\/p>\n<pre>++ took 8.1739809s\ny=y+1 took 17.9079386s<\/pre>\n<p>edit2\uff1a\u6309\u7167\u5efa\u8bae\uff0c\u6211\u8f6c\u50a8\u4e86\u673a\u5668\u4ee3\u7801\u3002\u4ee5\u4e0b\u662f\u76f8\u5173\u7247\u6bb5<\/p>\n<p>\u5bf9\u4e8e x[0]++<\/p>\n<pre>0x4981e3              488d05b6830100          leaq runtime.types+95648(sb), ax\n  0x4981ea              48890424                movq ax, 0(sp)\n  0x4981ee              488d8c2400020000        leaq 0x200(sp), cx\n  0x4981f6              48894c2408              movq cx, 0x8(sp)\n  0x4981fb              48c744241000000000      movq $0x0, 0x10(sp)\n  0x498204              e8976df7ff              call runtime.mapassign_fast64(sb)\n  0x498209              488b442418              movq 0x18(sp), ax\n  0x49820e              48ff00                  incq 0(ax)<\/pre>\n<p>\u5bf9\u4e8e y[0] = y[0] + 1<\/p>\n<pre>0x498302              488d0597820100          LEAQ runtime.types+95648(SB), AX\n  0x498309              48890424                MOVQ AX, 0(SP)\n  0x49830d              488d8c24d0010000        LEAQ 0x1d0(SP), CX\n  0x498315              48894c2408              MOVQ CX, 0x8(SP)\n  0x49831a              48c744241000000000      MOVQ $0x0, 0x10(SP)\n  0x498323              e80869f7ff              CALL runtime.mapaccess1_fast64(SB)\n  0x498328              488b442418              MOVQ 0x18(SP), AX\n  0x49832d              488b00                  MOVQ 0(AX), AX\n  0x498330              4889442448              MOVQ AX, 0x48(SP)\n  0x498335              488d0d64820100          LEAQ runtime.types+95648(SB), CX\n  0x49833c              48890c24                MOVQ CX, 0(SP)\n  0x498340              488d9424d0010000        LEAQ 0x1d0(SP), DX\n  0x498348              4889542408              MOVQ DX, 0x8(SP)\n  0x49834d              48c744241000000000      MOVQ $0x0, 0x10(SP)\n  0x498356              e8456cf7ff              CALL runtime.mapassign_fast64(SB)\n  0x49835b              488b442418              MOVQ 0x18(SP), AX\n  0x498360              488b4c2448              MOVQ 0x48(SP), CX\n  0x498365              48ffc1                  INCQ CX\n  0x498368              488908                  MOVQ CX, 0(AX)<\/pre>\n<p>\u5947\u602a\u7684\u662f\uff0c++ \u751a\u81f3\u4e0d\u8c03\u7528\u6620\u5c04\u8bbf\u95ee\uff01 ++ \u663e\u7136\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u64cd\u4f5c\uff0c\u6570\u91cf\u7ea7\u4e3a 2 \u6216 3\u3002\u6211\u89e3\u6790\u673a\u5668\u7684\u80fd\u529b\u5c31\u5230\u6b64\u4e3a\u6b62\uff0c\u6240\u4ee5\u5982\u679c\u6709\u4eba\u6df1\u5165\u4e86\u89e3\u6b63\u5728\u53d1\u751f\u7684\u4e8b\u60c5\uff0c\u6211\u5f88\u4e50\u610f\u542c\u5230\u5b83<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>go gc \u7f16\u8bd1\u5668\u662f\u4e00\u4e2a\u4f18\u5316\u7f16\u8bd1\u5668\u3002\u5b83\u6b63\u5728\u4e0d\u65ad\u6539\u8fdb\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8ego1.11\uff0c<\/p>\n<p>go\u95ee\u9898\uff1a<\/p>\n<p>\u63d0\u4ea4\uff1a<\/p>\n<p>\u4ee3\u7801\u7684\u7ed3\u679c\uff1a<\/p>\n<p><code>go1.10<\/code>\uff1a<\/p>\n<pre>++ took 10.258130907s\ny=y+1 took 10.233823639s<\/pre>\n<p><code>go1.11<\/code>\uff1a<\/p>\n<pre>++ took 7.995184419s\ny=y+1 took 10.259916484s<\/pre>\n<p>\u60a8\u7684\u95ee\u9898\u7684\u4e00\u822c\u7b54\u6848\u662f\u5728\u60a8\u7684\u4ee3\u7801\u4e2d\u7b80\u5355\u3001\u660e\u786e\u4e14\u660e\u663e\u3002\u7136\u540e\uff0c\u7f16\u8bd1\u5668\u53ef\u4ee5\u66f4\u8f7b\u677e\u5730\u8bc6\u522b\u5e38\u89c1\u7684\u53ef\u4f18\u5316\u6a21\u5f0f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207012","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207012","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207012"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207012\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207012"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207012"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207012"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}