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>\n5.\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}]}}