自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Go 中 Set 的實現(xiàn)方案,你會嗎?

網(wǎng)絡(luò) 通信技術(shù)
Go 的設(shè)計是一種簡單哲學(xué),它摒棄了其他語言一些臃腫的功能和模塊,以降低程序員的學(xué)習(xí)門檻,減少使用中的心智負擔(dān)。

Go 的設(shè)計是一種簡單哲學(xué),[[423390]]

它摒棄了其他語言一些臃腫的功能和模塊,以降低程序員的學(xué)習(xí)門檻,減少使用中的心智負擔(dān)。

本文,我們來探討 Go 中缺失的數(shù)據(jù)結(jié)構(gòu):Set,以及它的最佳實現(xiàn)方案。

Set 語義與實現(xiàn)方案

Set 集合是其他語言中常見的數(shù)據(jù)結(jié)構(gòu)。特性:集合中的對象不按特定的方式排序,并且沒有重復(fù)對象。

學(xué)習(xí) Go ,要記?。篏o 沒有包含的東西,不代表 Go 真的沒有。根據(jù) Set 特性,我們可以很輕松地想到使用 map 的實現(xiàn)方案(因為 map 的 key 是不重復(fù)的):把對象當(dāng)做 key 存入 map。

使用 map 來實現(xiàn) Set,意味著我們只關(guān)心 key 的存在,其 value 值并不重要。有其他語言編程經(jīng)驗的人也許會選擇 bool 來作為 value,因為它是其它語言中內(nèi)存消耗最少的類型(1個字節(jié))。但是在 Go 中,還有另一種選擇:struct{}。

  1. fmt.Println(unsafe.Sizeof(struct {}{})) // output: 0 

壓測對比

為了探究哪種數(shù)據(jù)結(jié)構(gòu)是作為 value 的最佳選擇。我們選擇了以下常用的類型作為 value 進行測試:bool、int、interface{}、struct{}。

  1. package main 
  2.  
  3. import ( 
  4.  "testing" 
  5.  
  6. const num = int(1 << 24) 
  7.  
  8. // 測試 bool 類型 
  9. func Benchmark_SetWithBoolValueWrite(b *testing.B) { 
  10.  set := make(map[int]bool) 
  11.  for i := 0; i < num; i++ { 
  12.   set[i] = true 
  13.  } 
  14.  
  15. // 測試 interface{} 類型 
  16. func Benchmark_SetWithInterfaceValueWrite(b *testing.B) { 
  17.  set := make(map[int]interface{}) 
  18.  for i := 0; i < num; i++ { 
  19.   set[i] = struct{}{} 
  20.  } 
  21.  
  22. // 測試 int 類型 
  23. func Benchmark_SetWithIntValueWrite(b *testing.B) { 
  24.  set := make(map[int]int
  25.  for i := 0; i < num; i++ { 
  26.   set[i] = 0 
  27.  } 
  28.  
  29. // 測試 struct{} 類型 
  30. func Benchmark_SetWithStructValueWrite(b *testing.B) { 
  31.  set := make(map[int]struct{}) 
  32.  for i := 0; i < num; i++ { 
  33.   set[i] = struct{}{} 
  34.  } 

我們運行以下命令,進行測試

  1. $ go test -v -bench=. -count=3 -benchmem | tee result.txt 
  2. goos: darwin 
  3. goarch: amd64 
  4. pkg: workspace/example/demoForSet 
  5. cpu: Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz 
  6. Benchmark_SetWithBoolValueWrite 
  7. Benchmark_SetWithBoolValueWrite-8                1 3549312568 ns/op 883610264 B/op   614311 allocs/op 
  8. Benchmark_SetWithBoolValueWrite-8                1 3288521519 ns/op 883599440 B/op   614206 allocs/op 
  9. Benchmark_SetWithBoolValueWrite-8                1 3264097496 ns/op 883578624 B/op   614003 allocs/op 
  10. Benchmark_SetWithInterfaceValueWrite 
  11. Benchmark_SetWithInterfaceValueWrite-8           1 4397757645 ns/op 1981619632 B/op   614062 allocs/op 
  12. Benchmark_SetWithInterfaceValueWrite-8           1 4088301215 ns/op 1981553392 B/op   613743 allocs/op 
  13. Benchmark_SetWithInterfaceValueWrite-8           1 3990698218 ns/op 1981560880 B/op   613773 allocs/op 
  14. Benchmark_SetWithIntValueWrite 
  15. Benchmark_SetWithIntValueWrite-8                 1 3472910194 ns/op 1412326480 B/op   615131 allocs/op 
  16. Benchmark_SetWithIntValueWrite-8                 1 3519755137 ns/op 1412187928 B/op   614294 allocs/op 
  17. Benchmark_SetWithIntValueWrite-8                 1 3459182691 ns/op 1412057672 B/op   613390 allocs/op 
  18. Benchmark_SetWithStructValueWrite 
  19. Benchmark_SetWithStructValueWrite-8              1 3126746088 ns/op 802452368 B/op   614127 allocs/op 
  20. Benchmark_SetWithStructValueWrite-8              1 3161650835 ns/op 802431240 B/op   613632 allocs/op 
  21. Benchmark_SetWithStructValueWrite-8              1 3160410871 ns/op 802440552 B/op   613748 allocs/op 
  22. PASS 
  23. ok   workspace/example/demoForSet 42.660s 

此時的結(jié)果看起來不太直觀,這里推薦一個 benchmark 統(tǒng)計工具:Benchstat。通過以下命令進行安裝

  1. $ go get -u golang.org/x/perf/cmd/benchstat 

使用 benchstat 分析剛才得到的 benchmark 結(jié)果文件

  1. $ benchstat result.txt 
  2. name                           time/op 
  3. _SetWithBoolValueWrite-8        3.37s ± 5% 
  4. _SetWithInterfaceValueWrite-8   4.16s ± 6% 
  5. _SetWithIntValueWrite-8         3.48s ± 1% 
  6. _SetWithStructValueWrite-8      3.15s ± 1% 
  7.  
  8. name                           alloc/op 
  9. _SetWithBoolValueWrite-8        884MB ± 0% 
  10. _SetWithInterfaceValueWrite-8  1.98GB ± 0% 
  11. _SetWithIntValueWrite-8        1.41GB ± 0% 
  12. _SetWithStructValueWrite-8      802MB ± 0% 
  13.  
  14. name                           allocs/op 
  15. _SetWithBoolValueWrite-8         614k ± 0% 
  16. _SetWithInterfaceValueWrite-8    614k ± 0% 
  17. _SetWithIntValueWrite-8          614k ± 0% 
  18. _SetWithStructValueWrite-8       614k ± 0% 

從內(nèi)存開銷而言,struct{} 是最小的,反映在執(zhí)行時間上也是最少的。由于 bool 類型僅占一個字節(jié),它相較于空結(jié)構(gòu)而言,相差的并不多。但是,如果使用 interface{} 類型,那差距就很明顯了。

所以,毫無疑問,在 Set 的實現(xiàn)中, map 值類型應(yīng)該選 struct{}。

總結(jié)

本文雖然討論的是 Set 的實現(xiàn)方案,但本質(zhì)是涉及空結(jié)構(gòu)體 struct{}{} 的 零內(nèi)存特性。

空結(jié)構(gòu)體除了是實現(xiàn) Set 的 value 值最佳方案,它還可以應(yīng)用于以下方面:

 

  • 通知信號的 channel:當(dāng) channel 只用于通知 goroutine 的執(zhí)行事件,此時 channel 就不需要發(fā)送任何實質(zhì)性的數(shù)據(jù),選擇使用 chan struct{} 。
  • 沒有狀態(tài)數(shù)據(jù)的結(jié)構(gòu)體:當(dāng)對象只擁有方法,而不包含任何的屬性字段時,選擇使用空結(jié)構(gòu)體定義該對象。

 

責(zé)任編輯:武曉燕 來源: Golang技術(shù)分享
相關(guān)推薦

2021-11-05 10:59:06

元編程語言工具

2022-03-15 08:36:46

遞歸查詢SQL

2021-10-27 10:55:18

Go入門Demo

2022-03-01 07:52:38

鏈表指針節(jié)點

2022-10-24 09:57:02

runeGo語言

2025-01-10 10:44:52

2019-05-07 15:49:27

AI人工智能藝術(shù)

2010-07-13 10:40:30

唐駿

2021-03-15 06:49:03

Ffmpeg項目轉(zhuǎn)換庫

2021-08-19 15:36:09

數(shù)據(jù)備份存儲備份策略

2021-11-10 15:37:49

Go源碼指令

2024-03-29 12:50:00

項目分層模型

2021-04-14 06:53:52

C# 修飾符 Public

2021-04-16 15:02:11

CAP理論分布式

2024-10-28 08:38:40

會員批量應(yīng)用

2021-02-15 14:48:31

Hive語法sql

2024-02-22 08:31:26

數(shù)據(jù)恢復(fù)工具MySQL回滾SQL

2012-06-20 10:47:25

Team Leader

2025-02-11 09:01:57

2024-08-22 08:50:51

點贊
收藏

51CTO技術(shù)棧公眾號