{"id":246676,"date":"2022-07-18T09:47:17","date_gmt":"2022-07-18T01:47:17","guid":{"rendered":"https:\/\/gulass.cn\/?p=246676"},"modified":"2022-07-04T13:20:03","modified_gmt":"2022-07-04T05:20:03","slug":"golang-three-linux","status":"publish","type":"post","link":"https:\/\/gulass.cn\/golang-three-linux.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdGolang\u5207\u7247\u5220\u9664\u6307\u5b9a\u5143\u7d20\u7684\u4e09\u79cd\u65b9\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nGo\u8bed\u8a00\u5e76\u6ca1\u6709\u63d0\u4f9b\u7528\u4e8e\u5220\u9664\u5143\u7d20\u7684\u8bed\u6cd5\u6216\u63a5\u53e3,\u800c\u662f\u901a\u8fc7\u5229\u7528\u5207\u7247\u672c\u8eab\u7684\u7279\u6027\u6765\u5220\u9664\u5143\u7d20\u2014\u8ffd\u52a0\u5143\u7d20,\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eGolang\u5207\u7247\u5220\u9664\u6307\u5b9a\u5143\u7d20\u7684\u4e09\u79cd\u65b9\u6cd5,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00<\/strong><\/div>\n

Go \u5e76\u6ca1\u6709\u63d0\u4f9b\u5220\u9664\u5207\u7247\u5143\u7d20\u4e13\u7528\u7684\u8bed\u6cd5\u6216\u51fd\u6570\uff0c\u9700\u8981\u4f7f\u7528\u5207\u7247\u672c\u8eab\u7684\u7279\u6027\u6765\u5220\u9664\u5143\u7d20\u3002<\/p>\n

\u5220\u9664\u5207\u7247\u6307\u5b9a\u5143\u7d20\u4e00\u822c\u6709\u5982\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff0c\u672c\u6587\u4ee5 []int \u4e3a\u4f8b\u7ed9\u51fa\u5177\u4f53\u5b9e\u73b0\u3002<\/p>\n

1.\u622a\u53d6\u6cd5\uff08\u4fee\u6539\u539f\u5207\u7247\uff09<\/strong><\/div>\n

\u8fd9\u91cc\u5229\u7528\u5bf9 slice \u7684\u622a\u53d6\u5220\u9664\u6307\u5b9a\u5143\u7d20\u3002\u6ce8\u610f\u5220\u9664\u65f6\uff0c\u540e\u9762\u7684\u5143\u7d20\u4f1a\u524d\u79fb\uff0c\u6240\u4ee5\u4e0b\u6807 i \u5e94\u8be5\u5de6\u79fb\u4e00\u4f4d\u3002<\/p>\n

\/\/ DeleteSlice1 \u5220\u9664\u6307\u5b9a\u5143\u7d20\u3002\r\nfunc DeleteSlice1(a []int, elem int) []int {\r\n    for i := 0; i < len(a); i++ {\r\n        if a[i] == elem {\r\n            a = append(a[:i], a[i+1:]...)\r\n            i--\r\n        }\r\n    }\r\n    return a\r\n}<\/pre>\n
2.\u62f7\u8d1d\u6cd5\uff08\u4e0d\u6539\u539f\u5207\u7247\uff09<\/strong><\/div>\n

\u8fd9\u79cd\u65b9\u6cd5\u6700\u5bb9\u6613\u7406\u89e3\uff0c\u91cd\u65b0\u4f7f\u7528\u4e00\u4e2a slice\uff0c\u5c06\u8981\u5220\u9664\u7684\u5143\u7d20\u8fc7\u6ee4\u6389\u3002\u7f3a\u70b9\u662f\u9700\u8981\u5f00\u8f9f\u53e6\u4e00\u4e2a slice \u7684\u7a7a\u95f4\uff0c\u4f18\u70b9\u662f\u5bb9\u6613\u7406\u89e3\uff0c\u800c\u4e14\u4e0d\u4f1a\u4fee\u6539\u539f slice\u3002<\/p>\n

\/\/ DeleteSlice2 \u5220\u9664\u6307\u5b9a\u5143\u7d20\u3002\r\nfunc DeleteSlice2(a []int, elem int) []int {\r\n    tmp := make([]int, 0, len(a))\r\n    for _, v := range a {\r\n        if v != elem {\r\n            tmp = append(tmp, v)\r\n        }\r\n    }\r\n    return tmp\r\n}<\/pre>\n
3.\u79fb\u4f4d\u6cd5\uff08\u4fee\u6539\u539f\u5207\u7247\uff09<\/strong><\/div>\n
3.1 \u65b9\u5f0f\u4e00<\/strong><\/span><\/div>\n

\u5229\u7528\u4e00\u4e2a\u4e0b\u6807 index\uff0c\u8bb0\u5f55\u4e0b\u4e00\u4e2a\u6709\u6548\u5143\u7d20\u5e94\u8be5\u5728\u7684\u4f4d\u7f6e\u3002\u904d\u5386\u6240\u6709\u5143\u7d20\uff0c\u5f53\u9047\u5230\u6709\u6548\u5143\u7d20\uff0c\u5c06\u5176\u79fb\u52a8\u5230 index \u4e14 index \u52a0\u4e00\u3002\u6700\u7ec8 index \u7684\u4f4d\u7f6e\u5c31\u662f\u6240\u6709\u6709\u6548\u5143\u7d20\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u6700\u540e\u505a\u4e00\u4e2a\u622a\u53d6\u5c31\u884c\u4e86\u3002\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u4fee\u6539\u539f\u6765\u7684 slice\u3002<\/p>\n

\u8be5\u65b9\u6cd5\u53ef\u4ee5\u770b\u6210\u5bf9\u7b2c\u4e00\u79cd\u65b9\u6cd5\u622a\u53d6\u6cd5\u7684\u6539\u8fdb\uff0c\u56e0\u4e3a\u6bcf\u6b21\u6307\u9700\u79fb\u52a8\u4e00\u4e2a\u5143\u7d20\uff0c\u6027\u80fd\u66f4\u52a0\u3002<\/p>\n

\/\/ DeleteSlice3 \u5220\u9664\u6307\u5b9a\u5143\u7d20\u3002\r\nfunc DeleteSlice3(a []int, elem int) []int {\r\n    j := 0\r\n    for _, v := range a {\r\n        if v != elem {\r\n            a[j] = v\r\n            j++\r\n        }\r\n    }\r\n    return a[:j]\r\n}<\/pre>\n
3.2 \u65b9\u5f0f\u4e8c<\/strong><\/span><\/div>\n

\u521b\u5efa\u4e86\u4e00\u4e2a slice\uff0c\u4f46\u662f\u5171\u7528\u539f\u59cb slice \u7684\u5e95\u5c42\u6570\u7ec4\u3002\u8fd9\u6837\u4e5f\u4e0d\u9700\u8981\u989d\u5916\u5206\u914d\u5185\u5b58\u7a7a\u95f4\uff0c\u76f4\u63a5\u5728\u539f slice \u4e0a\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n

\/\/ DeleteSlice4 \u5220\u9664\u6307\u5b9a\u5143\u7d20\u3002\r\nfunc DeleteSlice4(a []int, elem int) []int {\r\n    tgt := a[:0]\r\n    for _, v := range a {\r\n        if v != elem {\r\n            tgt = append(tgt, v)\r\n        }\r\n    }\r\n    return tgt\r\n}<\/pre>\n
4.\u6027\u80fd\u5bf9\u6bd4<\/strong><\/div>\n

\u5047\u8bbe\u6211\u4eec\u7684\u5207\u7247\u6709 0 \u548c 1\uff0c\u6211\u4eec\u8981\u5220\u9664\u6240\u6709\u7684 0\u3002<\/p>\n

\u8fd9\u91cc\u5206\u522b\u5bf9\u957f\u5ea6\u4e3a 10\u3001100\u30011000 \u7684\u5207\u7247\u8fdb\u884c\u6d4b\u8bd5\uff0c\u6765\u4e0a\u4e0b\u4e0a\u9762\u56db\u79cd\u5b9e\u73b0\u7684\u6027\u80fd\u5dee\u5f02\u3002<\/p>\n

\u751f\u6210\u5207\u7247\u51fd\u6570\u5982\u4e0b\uff1a<\/p>\n

func getSlice(n int) []int {\r\n    a := make([]int, 0, n)\r\n    for i := 0; i < n; i++ {\r\n        if i%2 == 0 {\r\n            a = append(a, 0)\r\n            continue\r\n        }\r\n        a = append(a, 1)\r\n    }\r\n    return a\r\n}<\/pre>\n

\u57fa\u51c6\u6d4b\u8bd5\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

func BenchmarkDeleteSlice1(b *testing.B) {\r\n    for i := 0; i < b.N; i++ {\r\n        _ = DeleteSlice1(getSlice(10), 0)\r\n    }\r\n}\r\nfunc BenchmarkDeleteSlice2(b *testing.B) {\r\n    for i := 0; i < b.N; i++ {\r\n        _ = DeleteSlice2(getSlice(10), 0)\r\n    }\r\n}\r\nfunc BenchmarkDeleteSlice3(b *testing.B) {\r\n    for i := 0; i < b.N; i++ {\r\n        _ = DeleteSlice3(getSlice(10), 0)\r\n    }\r\n}\r\nfunc BenchmarkDeleteSlice4(b *testing.B) {\r\n    for i := 0; i < b.N; i++ {\r\n        _ = DeleteSlice4(getSlice(10), 0)\r\n    }\r\n}<\/pre>\n

\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n

\u539f\u5207\u7247\u957f\u5ea6\u4e3a 10\uff1a<\/p>\n

go test -bench=. main\/slice\r\ngoos: windows\r\ngoarch: amd64\r\npkg: main\/slice\r\ncpu: Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz\r\nBenchmarkDeleteSlice1-8         17466486                65.07 ns\/op\r\nBenchmarkDeleteSlice2-8         14897282                85.22 ns\/op\r\nBenchmarkDeleteSlice3-8         21952129                50.78 ns\/op\r\nBenchmarkDeleteSlice4-8         22176390                54.68 ns\/op\r\nPASS\r\nok      main\/slice      5.427s<\/pre>\n

\u539f\u5207\u7247\u957f\u5ea6\u4e3a 100\uff1a<\/p>\n

BenchmarkDeleteSlice1-8          1652146               762.1 ns\/op\r\nBenchmarkDeleteSlice2-8          2124237               578.4 ns\/op\r\nBenchmarkDeleteSlice3-8          3161318               359.9 ns\/op\r\nBenchmarkDeleteSlice4-8          2714158               423.7 ns\/op<\/pre>\n

\u539f\u5207\u7247\u957f\u5ea6\u4e3a 1000\uff1a<\/p>\n

BenchmarkDeleteSlice1-8            56067             21915 ns\/op\r\nBenchmarkDeleteSlice2-8           258662              5007 ns\/op\r\nBenchmarkDeleteSlice3-8           432049              2724 ns\/op\r\nBenchmarkDeleteSlice4-8           325194              3615 ns\/op<\/pre>\n
5.\u5c0f\u7ed3<\/strong><\/div>\n

\u4ece\u57fa\u51c6\u6d4b\u8bd5\u7ed3\u679c\u6765\u770b\uff0c\u6027\u80fd\u6700\u4f73\u7684\u65b9\u6cd5\u662f\u79fb\u4f4d\u6cd5\uff0c\u5176\u4e2d\u53c8\u5c5e\u7b2c\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\u8f83\u4f73\u3002\u6027\u80fd\u6700\u5dee\u7684\u4e5f\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u622a\u53d6\u6cd5\u3002\u968f\u7740\u5207\u7247\u957f\u5ea6\u7684\u589e\u52a0\uff0c\u4e0a\u9762\u56db\u79cd\u5220\u9664\u65b9\u5f0f\u7684\u6027\u80fd\u5dee\u5f02\u4f1a\u6108\u52a0\u660e\u663e\u3002<\/p>\n

\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u4e0d\u7528\u573a\u666f\u6765\u9009\u62e9\u3002\u5982\u4e0d\u80fd\u4fee\u6539\u539f\u5207\u7247\u4f7f\u7528\u62f7\u8d1d\u6cd5\uff0c\u53ef\u4ee5\u4fee\u6539\u539f\u5207\u7247\u4f7f\u7528\u79fb\u4f4d\u6cd5\u4e2d\u7684\u7b2c\u4e00\u79cd\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Go \u5e76\u6ca1\u6709\u63d0\u4f9b\u5220\u9664\u5207\u7247\u5143\u7d20\u4e13\u7528\u7684\u8bed\u6cd5\u6216\u51fd\u6570\uff0c\u9700\u8981\u4f7f\u7528\u5207\u7247\u672c\u8eab\u7684\u7279\u6027\u6765\u5220\u9664\u5143\u7d20\u3002 \u5220\u9664\u5207\u7247\u6307\u5b9a\u5143\u7d20\u4e00\u822c\u6709\u5982\u4e0b\u51e0 […]<\/p>\n","protected":false},"author":1482,"featured_media":244612,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-246676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/246676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=246676"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/246676\/revisions"}],"predecessor-version":[{"id":246679,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/246676\/revisions\/246679"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/244612"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=246676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=246676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=246676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}