github.com/zhongdalu/gf@v1.0.0/g/container/gmap/gmap_z_bench_syncmap_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with gm file,
     5  // You can obtain one at https://github.com/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gmap_test
    10  
    11  import (
    12  	"sync"
    13  	"testing"
    14  
    15  	"github.com/zhongdalu/gf/g/container/gmap"
    16  )
    17  
    18  var gm = gmap.NewIntIntMap()
    19  var sm = sync.Map{}
    20  
    21  func Benchmark_GMapSet(b *testing.B) {
    22  	b.RunParallel(func(pb *testing.PB) {
    23  		i := 0
    24  		for pb.Next() {
    25  			gm.Set(i, i)
    26  			i++
    27  		}
    28  	})
    29  }
    30  
    31  func Benchmark_SyncMapSet(b *testing.B) {
    32  	b.RunParallel(func(pb *testing.PB) {
    33  		i := 0
    34  		for pb.Next() {
    35  			sm.Store(i, i)
    36  			i++
    37  		}
    38  	})
    39  }
    40  
    41  func Benchmark_GMapGet(b *testing.B) {
    42  	b.RunParallel(func(pb *testing.PB) {
    43  		i := 0
    44  		for pb.Next() {
    45  			gm.Get(i)
    46  			i++
    47  		}
    48  	})
    49  }
    50  
    51  func Benchmark_SyncMapGet(b *testing.B) {
    52  	b.RunParallel(func(pb *testing.PB) {
    53  		i := 0
    54  		for pb.Next() {
    55  			sm.Load(i)
    56  			i++
    57  		}
    58  	})
    59  }
    60  
    61  func Benchmark_GMapRemove(b *testing.B) {
    62  	b.RunParallel(func(pb *testing.PB) {
    63  		i := 0
    64  		for pb.Next() {
    65  			gm.Remove(i)
    66  			i++
    67  		}
    68  	})
    69  }
    70  
    71  func Benchmark_SyncMapRmove(b *testing.B) {
    72  	b.RunParallel(func(pb *testing.PB) {
    73  		i := 0
    74  		for pb.Next() {
    75  			sm.Delete(i)
    76  			i++
    77  		}
    78  	})
    79  }