2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-02-28 15:32:52 +08:00
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
2025-11-21 14:12:56 +08:00
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-02-28 15:32:52 +08:00
|
|
|
|
2018-09-20 09:28:45 +08:00
|
|
|
// go test *.go -bench=".*" -benchmem
|
2018-02-28 15:32:52 +08:00
|
|
|
|
|
|
|
|
package gmap_test
|
|
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"sync"
|
|
|
|
|
"testing"
|
2019-07-16 20:30:10 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/container/gmap"
|
2018-02-28 15:32:52 +08:00
|
|
|
)
|
|
|
|
|
|
2019-08-26 23:35:44 +08:00
|
|
|
var gm = gmap.NewIntIntMap(true)
|
2022-11-01 20:12:21 +08:00
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
var sm = sync.Map{}
|
2018-02-28 15:32:52 +08:00
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
func Benchmark_GMapSet(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
gm.Set(i, i)
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-28 15:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
func Benchmark_SyncMapSet(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
sm.Store(i, i)
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-28 15:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
func Benchmark_GMapGet(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
gm.Get(i)
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-28 15:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
func Benchmark_SyncMapGet(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
sm.Load(i)
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-28 15:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
func Benchmark_GMapRemove(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
gm.Remove(i)
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-28 15:32:52 +08:00
|
|
|
}
|
|
|
|
|
|
2019-07-16 20:30:10 +08:00
|
|
|
func Benchmark_SyncMapRmove(b *testing.B) {
|
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
|
i := 0
|
|
|
|
|
for pb.Next() {
|
|
|
|
|
sm.Delete(i)
|
|
|
|
|
i++
|
|
|
|
|
}
|
|
|
|
|
})
|
2018-02-28 15:32:52 +08:00
|
|
|
}
|