Files
gf/container/gset/gset_z_bench_test.go

179 lines
3.4 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2018-02-24 16:57:37 +08:00
//
// 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://github.com/gogf/gf.
2018-02-24 16:57:37 +08:00
// go test *.go -bench=".*" -benchmem
2018-02-24 16:57:37 +08:00
package gset_test
import (
2019-06-19 09:06:52 +08:00
"strconv"
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gset"
2018-02-24 16:57:37 +08:00
)
2019-08-26 23:35:44 +08:00
var intSet = gset.NewIntSet(true)
2019-08-26 23:35:44 +08:00
var anySet = gset.NewSet(true)
2019-09-05 11:38:36 +08:00
var strSet = gset.NewStrSet(true)
2019-08-26 23:35:44 +08:00
var intSetUnsafe = gset.NewIntSet()
2019-08-26 23:35:44 +08:00
var anySetUnsafe = gset.NewSet()
2019-09-05 11:38:36 +08:00
var strSetUnsafe = gset.NewStrSet()
2018-02-24 16:57:37 +08:00
func Benchmark_IntSet_Add(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
intSet.Add(i)
i++
}
})
2018-02-24 16:57:37 +08:00
}
func Benchmark_IntSet_Contains(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
intSet.Contains(i)
i++
}
})
2018-02-24 16:57:37 +08:00
}
func Benchmark_IntSet_Remove(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
intSet.Remove(i)
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_AnySet_Contains(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
anySet.Contains(i)
i++
}
})
}
func Benchmark_AnySet_Remove(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
i := 0
for pb.Next() {
anySet.Remove(i)
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++
}
})
}
// 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++
}
})
}
// 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++
}
})
2019-02-02 15:16:45 +08:00
}
func Benchmark_Unsafe_IntSet_Add(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
intSetUnsafe.Add(i)
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
func Benchmark_Unsafe_IntSet_Contains(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
intSetUnsafe.Contains(i)
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
func Benchmark_Unsafe_IntSet_Remove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
intSetUnsafe.Remove(i)
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
func Benchmark_Unsafe_AnySet_Add(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
anySetUnsafe.Add(i)
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
func Benchmark_Unsafe_AnySet_Contains(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
anySetUnsafe.Contains(i)
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
func Benchmark_Unsafe_AnySet_Remove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
anySetUnsafe.Remove(i)
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
// Note that there's additional performance cost for string conversion.
func Benchmark_Unsafe_StrSet_Add(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
strSetUnsafe.Add(strconv.Itoa(i))
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
// Note that there's additional performance cost for string conversion.
func Benchmark_Unsafe_StrSet_Contains(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
strSetUnsafe.Contains(strconv.Itoa(i))
2019-06-19 09:06:52 +08:00
}
2019-02-02 15:16:45 +08:00
}
// Note that there's additional performance cost for string conversion.
func Benchmark_Unsafe_StrSet_Remove(b *testing.B) {
2019-06-19 09:06:52 +08:00
for i := 0; i < b.N; i++ {
strSetUnsafe.Remove(strconv.Itoa(i))
2019-06-19 09:06:52 +08:00
}
}