Skip to content

Commit

Permalink
benchmark Go map for comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
metalim committed Aug 31, 2023
1 parent 4df650f commit fc0439c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import (

const (
BENCHMARK_SIMPLEMAP = true
BENCHMARK_GOMAP = true

SET_KEYS = 1e5
DELETE_KEYS = 1e4
GET_KEYS = 1e5

PREPARE_KEYS = 1e7
KEY_LEN = 10
LOG_KEY_STATS = true
LOG_KEY_STATS = false
)

func benchmarkSuite[T Map](b *testing.B, new func() T) {
Expand Down Expand Up @@ -144,6 +145,9 @@ func init() {
if BENCHMARK_SIMPLEMAP {
mapDefs = append(mapDefs, mapDef{"simplemap", func() Map { return simplemap.New() }})
}
if BENCHMARK_GOMAP {
mapDefs = append(mapDefs, mapDef{"gomap", func() Map { return GoMap{} }})
}
}

var ops = []struct {
Expand Down Expand Up @@ -179,3 +183,18 @@ func Benchmark(b *testing.B) {
}
})
}

// to compare map[string]any with jsonmap
type GoMap map[string]any

func (m GoMap) Len() int { return len(m) }
func (m GoMap) Set(k string, v any) { m[k] = v }
func (m GoMap) Get(k string) (any, bool) { v, ok := m[k]; return v, ok }
func (m GoMap) Delete(k string) { delete(m, k) }
func (m GoMap) Keys() []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}

0 comments on commit fc0439c

Please sign in to comment.