-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show a small slice has better performance than a map
- Loading branch information
1 parent
35c0956
commit ec8ebe4
Showing
3 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
module github.com/GeniusesGroup/go-benchmarks | ||
|
||
go 1.19 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/GeniusesGroup/libgo v0.0.0-20220713101832-828057a9d3d4 | ||
github.com/GeniusesGroup/memar-go v0.0.0-20230923141417-dbb38f750948 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* For license and copyright information please see the LEGAL file in the code repository */ | ||
|
||
package ms | ||
|
||
import "strconv" | ||
|
||
const kvNumber = 16 | ||
const notExistKey = "NotExistKey" | ||
|
||
type smallMap map[string]string | ||
|
||
func (sm *smallMap) Init() { | ||
*sm = make(smallMap, kvNumber) | ||
} | ||
func (sm smallMap) Fill() { | ||
for i := 0; i < kvNumber; i++ { | ||
var index = strconv.Itoa(i) | ||
sm[index] = index | ||
} | ||
} | ||
func (ss smallMap) GetMiddle() string { | ||
var middle = kvNumber / 2 | ||
var middleIndex = strconv.Itoa(middle) | ||
return ss[middleIndex] | ||
} | ||
func (ss smallMap) GetNotExist() string { | ||
return ss[notExistKey] | ||
} | ||
|
||
type smallSlice []smallSlice_KV | ||
|
||
type smallSlice_KV struct { | ||
key string | ||
value string | ||
} | ||
|
||
func (ss *smallSlice) Init() { | ||
*ss = make(smallSlice, kvNumber) | ||
} | ||
func (ss smallSlice) Fill() { | ||
for i := 0; i < kvNumber; i++ { | ||
var index = strconv.Itoa(i) | ||
ss[i] = smallSlice_KV{index, index} | ||
} | ||
} | ||
func (ss smallSlice) Find(key string) string { | ||
for i := 0; i < kvNumber; i++ { | ||
if ss[i].key == key { | ||
return ss[i].value | ||
} | ||
} | ||
return "" | ||
} | ||
func (ss smallSlice) GetMiddle() string { | ||
var middle = kvNumber / 2 | ||
var middleIndex = strconv.Itoa(middle) | ||
return ss.Find(middleIndex) | ||
} | ||
func (ss smallSlice) GetNotExist() string { | ||
return ss.Find(notExistKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* For license and copyright information please see the LEGAL file in the code repository */ | ||
|
||
package ms | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
/* | ||
Number of CPU used: 8 | ||
goos: windows | ||
goarch: amd64 | ||
pkg: github.com/GeniusesGroup/go-benchmarks/map-vs-slice | ||
cpu: Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz | ||
Benchmark_smallMap_Insert-8 995197 1253 ns/op 1208 B/op 2 allocs/op | ||
Benchmark_smallSlice_Insert-8 3574993 334.6 ns/op 512 B/op 1 allocs/op | ||
Benchmark_smallMap_GetMiddle-8 898304 1267 ns/op 1208 B/op 2 allocs/op | ||
Benchmark_smallSlice_GetMiddle-8 2907439 430.9 ns/op 512 B/op 1 allocs/op | ||
Benchmark_smallMap_GetNotExist-8 908842 1359 ns/op 1208 B/op 2 allocs/op | ||
Benchmark_smallSlice_GetNotExist-8 3342152 354.9 ns/op 512 B/op 1 allocs/op | ||
*/ | ||
|
||
func init() { | ||
fmt.Println("Number of CPU used:", runtime.NumCPU()) | ||
} | ||
|
||
/* | ||
Benchmarks | ||
*/ | ||
|
||
func Benchmark_smallMap_Insert(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
var sm smallMap | ||
sm.Init() | ||
sm.Fill() | ||
} | ||
} | ||
func Benchmark_smallSlice_Insert(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
var ss smallSlice | ||
ss.Init() | ||
ss.Fill() | ||
} | ||
} | ||
|
||
func Benchmark_smallMap_GetMiddle(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
var sm smallMap | ||
sm.Init() | ||
sm.Fill() | ||
sm.GetMiddle() | ||
} | ||
} | ||
func Benchmark_smallSlice_GetMiddle(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
var ss smallSlice | ||
ss.Init() | ||
ss.Fill() | ||
ss.GetMiddle() | ||
} | ||
} | ||
|
||
func Benchmark_smallMap_GetNotExist(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
var sm smallMap | ||
sm.Init() | ||
sm.Fill() | ||
sm.GetNotExist() | ||
} | ||
} | ||
func Benchmark_smallSlice_GetNotExist(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
var ss smallSlice | ||
ss.Init() | ||
ss.Fill() | ||
ss.GetNotExist() | ||
} | ||
} |