코딩/GoLang

GoLang 강좌 - Slice 와 Map 벤치마크 테스트

드리프트 2021. 2. 10. 21:20
728x170

 

 

고랭에서 자주 쓰는 데이터 객체인 Slice 와 Map 자료형에 대한 벤치마크 테스트입니다.

 

고랭 공부에 많은 도움이 되셨으면 합니다.

 

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)
	}
}

 

테스트 결과입니다.

C:\golang\codes>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

 

압도적으로 Slice가 빠릅니다. 약 5배 차이가 나는군요.

그리드형