improve benchmark test cases for gset/glist

This commit is contained in:
John
2019-07-16 20:45:57 +08:00
parent ac6a8b9b74
commit 5ab64e31fd
3 changed files with 129 additions and 79 deletions

View File

@ -13,41 +13,49 @@ import (
)
var (
l = New()
bn = 20000000
l = New()
)
func Benchmark_PushBack(b *testing.B) {
b.N = bn
for i := 0; i < b.N; i++ {
l.PushBack(i)
}
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
l.PushBack(i)
i++
}
})
}
func Benchmark_PushFront(b *testing.B) {
b.N = bn
for i := 0; i < b.N; i++ {
l.PushFront(i)
}
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
l.PushFront(i)
i++
}
})
}
func Benchmark_Len(b *testing.B) {
b.N = bn
for i := 0; i < b.N; i++ {
l.Len()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
l.Len()
}
})
}
func Benchmark_PopFront(b *testing.B) {
b.N = bn
for i := 0; i < b.N; i++ {
l.PopFront()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
l.PopFront()
}
})
}
func Benchmark_PopBack(b *testing.B) {
b.N = bn
for i := 0; i < b.N; i++ {
l.PopBack()
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
l.PopBack()
}
})
}

View File

@ -27,7 +27,6 @@ func Benchmark_HashMap_Set(b *testing.B) {
i++
}
})
}
func Benchmark_ListMap_Set(b *testing.B) {

View File

@ -4,127 +4,170 @@
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*"
// go test *.go -bench=".*" -benchmem
package gset_test
import (
"github.com/gogf/gf/g/container/gset"
"strconv"
"testing"
"github.com/gogf/gf/g/container/gset"
)
var ints = gset.NewIntSet()
var itfs = gset.NewSet()
var strs = gset.NewStringSet()
var intsUnsafe = gset.NewIntSet(true)
var itfsUnsafe = gset.NewSet(true)
var strsUnsafe = gset.NewStringSet(true)
var intSet = gset.NewIntSet()
var anySet = gset.NewSet()
var strSet = gset.NewStringSet()
var intSetUnsafe = gset.NewIntSet(true)
var anySetUnsafe = gset.NewSet(true)
var strSetUnsafe = gset.NewStringSet(true)
func Benchmark_IntSet_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
ints.Add(i)
}
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
intSet.Add(i)
i++
}
})
}
func Benchmark_IntSet_Contains(b *testing.B) {
for i := 0; i < b.N; i++ {
ints.Contains(i)
}
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
intSet.Contains(i)
i++
}
})
}
func Benchmark_IntSet_Remove(b *testing.B) {
for i := 0; i < b.N; i++ {
ints.Remove(i)
}
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
intSet.Remove(i)
i++
}
})
}
func Benchmark_Set_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
itfs.Add(i)
}
func Benchmark_AnySet_Add(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
anySet.Add(i)
i++
}
})
}
func Benchmark_Set_Contains(b *testing.B) {
for i := 0; i < b.N; i++ {
itfs.Contains(i)
}
func Benchmark_AnySet_Contains(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
anySet.Contains(i)
i++
}
})
}
func Benchmark_Set_Remove(b *testing.B) {
for i := 0; i < b.N; i++ {
itfs.Remove(i)
}
func Benchmark_AnySet_Remove(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
anySet.Remove(i)
i++
}
})
}
func Benchmark_StringSet_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
strs.Add(strconv.Itoa(i))
}
// Note that there's additional performance cost for string conversion.
func Benchmark_StrSet_Add(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
strSet.Add(strconv.Itoa(i))
i++
}
})
}
func Benchmark_StringSet_Contains(b *testing.B) {
for i := 0; i < b.N; i++ {
strs.Contains(strconv.Itoa(i))
}
// Note that there's additional performance cost for string conversion.
func Benchmark_StrSet_Contains(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
strSet.Contains(strconv.Itoa(i))
i++
}
})
}
func Benchmark_StringSet_Remove(b *testing.B) {
for i := 0; i < b.N; i++ {
strs.Remove(strconv.Itoa(i))
}
// Note that there's additional performance cost for string conversion.
func Benchmark_StrSet_Remove(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
strSet.Remove(strconv.Itoa(i))
i++
}
})
}
func Benchmark_Unsafe_IntSet_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
intsUnsafe.Add(i)
intSetUnsafe.Add(i)
}
}
func Benchmark_Unsafe_IntSet_Contains(b *testing.B) {
for i := 0; i < b.N; i++ {
intsUnsafe.Contains(i)
intSetUnsafe.Contains(i)
}
}
func Benchmark_Unsafe_IntSet_Remove(b *testing.B) {
for i := 0; i < b.N; i++ {
intsUnsafe.Remove(i)
intSetUnsafe.Remove(i)
}
}
func Benchmark_Unsafe_Set_Add(b *testing.B) {
func Benchmark_Unsafe_AnySet_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
itfsUnsafe.Add(i)
anySetUnsafe.Add(i)
}
}
func Benchmark_Unsafe_Set_Contains(b *testing.B) {
func Benchmark_Unsafe_AnySet_Contains(b *testing.B) {
for i := 0; i < b.N; i++ {
itfsUnsafe.Contains(i)
anySetUnsafe.Contains(i)
}
}
func Benchmark_Unsafe_Set_Remove(b *testing.B) {
func Benchmark_Unsafe_AnySet_Remove(b *testing.B) {
for i := 0; i < b.N; i++ {
itfsUnsafe.Remove(i)
anySetUnsafe.Remove(i)
}
}
func Benchmark_Unsafe_StringSet_Add(b *testing.B) {
// Note that there's additional performance cost for string conversion.
func Benchmark_Unsafe_StrSet_Add(b *testing.B) {
for i := 0; i < b.N; i++ {
strsUnsafe.Add(strconv.Itoa(i))
strSetUnsafe.Add(strconv.Itoa(i))
}
}
func Benchmark_Unsafe_StringSet_Contains(b *testing.B) {
// Note that there's additional performance cost for string conversion.
func Benchmark_Unsafe_StrSet_Contains(b *testing.B) {
for i := 0; i < b.N; i++ {
strsUnsafe.Contains(strconv.Itoa(i))
strSetUnsafe.Contains(strconv.Itoa(i))
}
}
func Benchmark_Unsafe_StringSet_Remove(b *testing.B) {
// Note that there's additional performance cost for string conversion.
func Benchmark_Unsafe_StrSet_Remove(b *testing.B) {
for i := 0; i < b.N; i++ {
strsUnsafe.Remove(strconv.Itoa(i))
strSetUnsafe.Remove(strconv.Itoa(i))
}
}