新增gset性能测试

This commit is contained in:
John
2018-02-24 16:57:37 +08:00
parent dc044ad1b4
commit ec204c3b92
4 changed files with 87 additions and 89 deletions

View File

@ -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))
}
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}