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
How to get struct variable information using reflect package?
How to write backslash in Golang string?
How to blur an image in Golang?
Simple function with return value in Golang
How to Remove duplicate values from Slice?
How to check if a string contains a numbers in Golang?
Dereferencing a pointer from another package
Different ways to validate JSON string
How to print string with double quote in Go?
Example of Sscan vs Sscanf vs Sscanln from FMT Package