Golang Slice vs Map Benchmark Testing
Example
package main
import (
"fmt"
"time"
)
type s struct {
s []*s
}
type m struct {
m map[int]*m
}
const (
testLevel = 1000000
testTimes = 600
)
func buildStruct() *s {
root := &s{}
cur := root
for i := 0; i < testLevel; i++ {
cur.s = make([]*s, 1)
cur.s[0] = &s{}
cur = cur.s[0]
}
return root
}
func traverseStruct(t *s) int {
levels := 0
for {
if t.s == nil {
return levels
}
t = t.s[0]
levels++
}
}
func buildMap() *m {
root := &m{}
cur := root
for i := 0; i < testLevel; i++ {
cur.m = make(map[int]*m, 1)
cur.m[0] = &m{}
cur = cur.m[0]
}
return root
}
func traverseMap(t *m) int {
levels := 0
for {
current, found := t.m[0]
if !found {
return levels
}
t = current
levels++
}
}
func main() {
fmt.Println("-----------Let's Start Building-------------")
testStruct := buildStruct()
testMap := buildMap()
fmt.Println("Traversing", testStruct, testMap)
for i := 0; i < testTimes; i++ {
start := time.Now()
sCount := traverseStruct(testStruct)
sEnd := time.Since(start)
start = time.Now()
mCount := traverseMap(testMap)
mEnd := time.Since(start)
if sCount != mCount {
panic("Count are different")
}
fmt.Printf("Traverse took: Slice %s vs Map %s\r\n", sEnd, mEnd)
}
}
Output
go run example301.go | more
-----------Let's Start Building-------------
Traversing &{[0x11452100]} &{map[0:0x12b69300]}
Traverse took: Slice 6.0003ms vs Map 24.0014ms
Traverse took: Slice 4.0002ms vs Map 90.0051ms
Traverse took: Slice 5.0003ms vs Map 23.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 23.0013ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 36.002ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 24.0013ms
Traverse took: Slice 4.0003ms vs Map 25.0014ms
Traverse took: Slice 5.0003ms vs Map 26.0015ms
Traverse took: Slice 5.0003ms vs Map 23.0013ms
Traverse took: Slice 4.0003ms vs Map 24.0013ms
Traverse took: Slice 4.0003ms vs Map 24.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 24.0013ms
Traverse took: Slice 4.0003ms vs Map 24.0013ms
Traverse took: Slice 4.0003ms vs Map 25.0014ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 23.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 24.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
----------------------------------------------
Traverse took: Slice 5.0003ms vs Map 25.0014ms
Traverse took: Slice 4.0002ms vs Map 25.0015ms
Traverse took: Slice 5.0002ms vs Map 25.0015ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 6.0003ms vs Map 24.0014ms
Traverse took: Slice 4.0002ms vs Map 25.0015ms
Traverse took: Slice 4.0002ms vs Map 25.0014ms
Traverse took: Slice 4.0002ms vs Map 25.0015ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 24.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 4.0003ms vs Map 24.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 25.0014ms
Traverse took: Slice 5.0003ms vs Map 24.0013ms
Traverse took: Slice 5.0003ms vs Map 24.0014ms
Traverse took: Slice 5.0003ms vs Map 26.0015ms
Traverse took: Slice 4.0002ms vs Map 24.0014ms
Most Helpful This Week
Golang import package inside package
Regular expression to validate email address
How to fix race condition using Atomic Functions in Golang?
How to get the current date and time with timestamp in local and other timezones ?
How to check lowercase characters in a string in Golang?
How to count number of repeating words in a given String?