diff --git a/g/container/gset/gset_test.go b/g/container/gset/gset_test.go new file mode 100644 index 000000000..7ac049e00 --- /dev/null +++ b/g/container/gset/gset_test.go @@ -0,0 +1,73 @@ +// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://gitee.com/johng/gf. + +// go test *.go -bench=".*" + +package gset_test + +import ( + "testing" + "strconv" + "gitee.com/johng/gf/g/container/gset" +) + +var ints = gset.NewIntSet() +var itfs = gset.NewInterfaceSet() +var strs = gset.NewStringSet() + +func BenchmarkIntSet_Add(b *testing.B) { + for i := 0; i < b.N; i++ { + ints.Add(i) + } +} + +func BenchmarkIntSet_Contains(b *testing.B) { + for i := 0; i < b.N; i++ { + ints.Contains(i) + } +} + +func BenchmarkIntSet_Remove(b *testing.B) { + for i := 0; i < b.N; i++ { + ints.Remove(i) + } +} + +func BenchmarkInterfaceSet_Add(b *testing.B) { + for i := 0; i < b.N; i++ { + itfs.Add(i) + } +} + +func BenchmarkInterfaceSet_Contains(b *testing.B) { + for i := 0; i < b.N; i++ { + itfs.Contains(i) + } +} + +func BenchmarkInterfaceSet_Remove(b *testing.B) { + for i := 0; i < b.N; i++ { + itfs.Remove(i) + } +} + +func BenchmarkStringSet_Add(b *testing.B) { + for i := 0; i < b.N; i++ { + strs.Add(strconv.Itoa(i)) + } +} + +func BenchmarkStringSet_Contains(b *testing.B) { + for i := 0; i < b.N; i++ { + strs.Contains(strconv.Itoa(i)) + } +} + +func BenchmarkStringSet_Remove(b *testing.B) { + for i := 0; i < b.N; i++ { + strs.Remove(strconv.Itoa(i)) + } +} \ No newline at end of file diff --git a/g/container/gset/int_set.go b/g/container/gset/int_set.go index 470852477..0efbeaf54 100644 --- a/g/container/gset/int_set.go +++ b/g/container/gset/int_set.go @@ -33,9 +33,6 @@ func (this *IntSet) Iterator(f func (v int)) { // 设置键 func (this *IntSet) Add(item int) *IntSet { - if this.Contains(item) { - return this - } this.mu.Lock() this.m[item] = struct{}{} this.mu.Unlock() @@ -44,33 +41,11 @@ func (this *IntSet) Add(item int) *IntSet { // 批量添加设置键 func (this *IntSet) BatchAdd(items []int) *IntSet { - count := len(items) - if count == 0 { - return this - } - - todo := make([]int, 0, count) - this.mu.RLock() - for i := 0; i < count; i++ { - _, exists := this.m[items[i]] - if exists { - continue - } - - todo = append(todo, items[i]) - } - this.mu.RUnlock() - - count = len(todo) - if count == 0 { - return this - } - - this.mu.Lock() - for i := 0; i < count; i++ { - this.m[todo[i]] = struct{}{} - } - this.mu.Unlock() + this.mu.Lock() + for _, item := range items { + this.m[item] = struct{}{} + } + this.mu.Unlock() return this } diff --git a/g/container/gset/interface_set.go b/g/container/gset/interface_set.go index 85f5a482d..93f8dcc3d 100644 --- a/g/container/gset/interface_set.go +++ b/g/container/gset/interface_set.go @@ -32,9 +32,6 @@ func (this *InterfaceSet) Iterator(f func (v interface{})) { // 添加 func (this *InterfaceSet) Add(item interface{}) *InterfaceSet { - if this.Contains(item) { - return this - } this.mu.Lock() this.m[item] = struct{}{} this.mu.Unlock() @@ -43,33 +40,11 @@ func (this *InterfaceSet) Add(item interface{}) *InterfaceSet { // 批量添加 func (this *InterfaceSet) BatchAdd(items []interface{}) *InterfaceSet { - count := len(items) - if count == 0 { - return this - } - - todo := make([]interface{}, 0, count) - this.mu.RLock() - for i := 0; i < count; i++ { - _, exists := this.m[items[i]] - if exists { - continue - } - - todo = append(todo, items[i]) - } - this.mu.RUnlock() - - count = len(todo) - if count == 0 { - return this - } - - this.mu.Lock() - for i := 0; i < count; i++ { - this.m[todo[i]] = struct{}{} - } - this.mu.Unlock() + this.mu.Lock() + for _, item := range items { + this.m[item] = struct{}{} + } + this.mu.Unlock() return this } diff --git a/g/container/gset/string_set.go b/g/container/gset/string_set.go index ec2c1fbf4..f50bc7a49 100644 --- a/g/container/gset/string_set.go +++ b/g/container/gset/string_set.go @@ -32,9 +32,6 @@ func (this *StringSet) Iterator(f func (v string)) { // 设置键 func (this *StringSet) Add(item string) *StringSet { - if this.Contains(item) { - return this - } this.mu.Lock() this.m[item] = struct{}{} this.mu.Unlock() @@ -43,33 +40,11 @@ func (this *StringSet) Add(item string) *StringSet { // 批量添加设置键 func (this *StringSet) BatchAdd(items []string) *StringSet { - count := len(items) - if count == 0 { - return this + this.mu.Lock() + for _, item := range items { + this.m[item] = struct{}{} } - - todo := make([]string, 0, count) - this.mu.RLock() - for i := 0; i < count; i++ { - _, exists := this.m[items[i]] - if exists { - continue - } - - todo = append(todo, items[i]) - } - this.mu.RUnlock() - - count = len(todo) - if count == 0 { - return this - } - - this.mu.Lock() - for i := 0; i < count; i++ { - this.m[todo[i]] = struct{}{} - } - this.mu.Unlock() + this.mu.Unlock() return this }