網站首頁 編程語言 正文
前言
Go 并沒有提供刪除切片元素專用的語法或函數,需要使用切片本身的特性來刪除元素。
刪除切片指定元素一般有如下幾種方法,本文以 []int 為例給出具體實現。
1.截取法(修改原切片)
這里利用對 slice 的截取刪除指定元素。注意刪除時,后面的元素會前移,所以下標 i 應該左移一位。
// DeleteSlice1 刪除指定元素。 func DeleteSlice1(a []int, elem int) []int { for i := 0; i < len(a); i++ { if a[i] == elem { a = append(a[:i], a[i+1:]...) i-- } } return a }
2.拷貝法(不改原切片)
這種方法最容易理解,重新使用一個 slice,將要刪除的元素過濾掉。缺點是需要開辟另一個 slice 的空間,優點是容易理解,而且不會修改原 slice。
// DeleteSlice2 刪除指定元素。 func DeleteSlice2(a []int, elem int) []int { tmp := make([]int, 0, len(a)) for _, v := range a { if v != elem { tmp = append(tmp, v) } } return tmp }
3.移位法(修改原切片)
3.1 方式一
利用一個下標 index,記錄下一個有效元素應該在的位置。遍歷所有元素,當遇到有效元素,將其移動到 index 且 index 加一。最終 index 的位置就是所有有效元素的下一個位置,最后做一個截取就行了。這種方法會修改原來的 slice。
該方法可以看成對第一種方法截取法的改進,因為每次指需移動一個元素,性能更加。
// DeleteSlice3 刪除指定元素。 func DeleteSlice3(a []int, elem int) []int { j := 0 for _, v := range a { if v != elem { a[j] = v j++ } } return a[:j] }
3.2 方式二
創建了一個 slice,但是共用原始 slice 的底層數組。這樣也不需要額外分配內存空間,直接在原 slice 上進行修改。
// DeleteSlice4 刪除指定元素。 func DeleteSlice4(a []int, elem int) []int { tgt := a[:0] for _, v := range a { if v != elem { tgt = append(tgt, v) } } return tgt }
4.性能對比
假設我們的切片有 0 和 1,我們要刪除所有的 0。
這里分別對長度為 10、100、1000 的切片進行測試,來上下上面四種實現的性能差異。
生成切片函數如下:
func getSlice(n int) []int { a := make([]int, 0, n) for i := 0; i < n; i++ { if i%2 == 0 { a = append(a, 0) continue } a = append(a, 1) } return a }
基準測試代碼如下:
func BenchmarkDeleteSlice1(b *testing.B) { for i := 0; i < b.N; i++ { _ = DeleteSlice1(getSlice(10), 0) } } func BenchmarkDeleteSlice2(b *testing.B) { for i := 0; i < b.N; i++ { _ = DeleteSlice2(getSlice(10), 0) } } func BenchmarkDeleteSlice3(b *testing.B) { for i := 0; i < b.N; i++ { _ = DeleteSlice3(getSlice(10), 0) } } func BenchmarkDeleteSlice4(b *testing.B) { for i := 0; i < b.N; i++ { _ = DeleteSlice4(getSlice(10), 0) } }
測試結果如下:
原切片長度為 10:
go test -bench=. main/slice
goos: windows
goarch: amd64
pkg: main/slice
cpu: Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
BenchmarkDeleteSlice1-8 ? ? ? ? 17466486 ? ? ? ? ? ? ? ?65.07 ns/op
BenchmarkDeleteSlice2-8 ? ? ? ? 14897282 ? ? ? ? ? ? ? ?85.22 ns/op
BenchmarkDeleteSlice3-8 ? ? ? ? 21952129 ? ? ? ? ? ? ? ?50.78 ns/op
BenchmarkDeleteSlice4-8 ? ? ? ? 22176390 ? ? ? ? ? ? ? ?54.68 ns/op
PASS
ok ? ? ?main/slice ? ? ?5.427s
原切片長度為 100:
BenchmarkDeleteSlice1-8 ? ? ? ? ?1652146 ? ? ? ? ? ? ? 762.1 ns/op
BenchmarkDeleteSlice2-8 ? ? ? ? ?2124237 ? ? ? ? ? ? ? 578.4 ns/op
BenchmarkDeleteSlice3-8 ? ? ? ? ?3161318 ? ? ? ? ? ? ? 359.9 ns/op
BenchmarkDeleteSlice4-8 ? ? ? ? ?2714158 ? ? ? ? ? ? ? 423.7 ns/op
原切片長度為 1000:
BenchmarkDeleteSlice1-8 ? ? ? ? ? ?56067 ? ? ? ? ? ? 21915 ns/op
BenchmarkDeleteSlice2-8 ? ? ? ? ? 258662 ? ? ? ? ? ? ?5007 ns/op
BenchmarkDeleteSlice3-8 ? ? ? ? ? 432049 ? ? ? ? ? ? ?2724 ns/op
BenchmarkDeleteSlice4-8 ? ? ? ? ? 325194 ? ? ? ? ? ? ?3615 ns/op
5.小結
從基準測試結果來看,性能最佳的方法是移位法,其中又屬第一種實現方式較佳。性能最差的也是最常用的方法是截取法。隨著切片長度的增加,上面四種刪除方式的性能差異會愈加明顯。
實際使用時,我們可以根據不用場景來選擇。如不能修改原切片使用拷貝法,可以修改原切片使用移位法中的第一種實現方式。
參考文獻
- golang刪除slice中特定條件的元素,優化版
- 【Golang】slice刪除元素的性能對比
原文鏈接:https://blog.csdn.net/K346K346/article/details/124616633
相關推薦
- 2023-04-06 C++之list容器介紹及使用方式_C 語言
- 2022-11-21 Golang?Mutex互斥鎖源碼分析_Golang
- 2022-04-04 react 報錯Assign arrow function to a variable before
- 2022-09-26 在?React?Native?中使用?CSS?Modules的配置方法_React
- 2023-07-14 express+element實現上傳圖片
- 2023-06-18 C#?指針內存控制Marshal內存數據存儲原理分析_C#教程
- 2022-11-05 docker中nginx卸載、安裝、配置及掛載詳細教程_docker
- 2022-04-24 .NET?CORE?鑒權的實現示例_實用技巧
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支