Last active
August 29, 2015 14:05
-
-
Save Shaked/e46aa01741edf51dda6a to your computer and use it in GitHub Desktop.
MapsVsSlicesVsArrays
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
package main | |
import "math/rand" | |
var amount = 1000 | |
func main() { | |
deferRecoverPanicMap() | |
deferRecoverPanicSliceExpandable() | |
deferRecoverPanicSliceFixedSize() | |
deferRecoverPanicArray() | |
keys := randomKeys() | |
searchMap(keys, runMap()) | |
searchSliceExpandable(keys, runSliceExpandable()) | |
searchSliceFixedSize(keys, runSliceFixedSize()) | |
searchArray(keys, runArray()) | |
} | |
func randomKeys() []int { | |
r := rand.New(rand.NewSource(99)) | |
return r.Perm(1000) | |
} | |
func deferRecoverPanicMap() { | |
m := runMap() | |
stub(m[40000]) | |
} | |
func deferRecoverPanicSliceExpandable() { | |
defer func() { | |
if r := recover(); r != nil { | |
} | |
}() | |
m := runSliceExpandable() | |
stub(m[40000]) | |
} | |
func deferRecoverPanicSliceFixedSize() { | |
defer func() { | |
if r := recover(); r != nil { | |
} | |
}() | |
m := runSliceFixedSize() | |
stub(m[40000]) | |
} | |
func deferRecoverPanicArray() { | |
defer func() { | |
if r := recover(); r != nil { | |
} | |
}() | |
m := runArray() | |
i := len(m) + 1 | |
stub(m[i]) | |
} | |
func runMap() map[int]string { | |
m := make(map[int]string) | |
for i := 0; i < amount; i++ { | |
m[i] = "someKey" | |
} | |
return m | |
} | |
func runSliceExpandable() []string { | |
var m []string | |
for i := 0; i < amount; i++ { | |
m = append(m, "someKey") | |
} | |
return m | |
} | |
func runSliceFixedSize() []string { | |
m := make([]string, amount) | |
for i := 0; i < amount; i++ { | |
m[i] = "someKey" | |
} | |
return m | |
} | |
func runArray() [1000]string { | |
var m [1000]string | |
for i := 0; i < amount; i++ { | |
m[i] = "someKey" | |
} | |
return m | |
} | |
func searchMap(keys []int, m map[int]string) { | |
for _, i := range keys { | |
stub(m[i]) | |
} | |
} | |
func searchSliceExpandable(keys []int, m []string) { | |
for _, i := range keys { | |
stub(m[i]) | |
} | |
} | |
func searchSliceFixedSize(keys []int, m []string) { | |
for _, i := range keys { | |
stub(m[i]) | |
} | |
} | |
func searchArray(keys []int, m [1000]string) { | |
for _, i := range keys { | |
stub(m[i]) | |
} | |
} | |
func deleteMap(m map[int]string) { | |
for i := 0; i < amount; i++ { | |
delete(m, i) | |
} | |
} | |
func deleteSlicePreservingOrder(m []string) { | |
for i := len(m) - 1; i >= 0; i-- { | |
copy(m[i:], m[i+1:]) | |
m = m[:len(m)-1] | |
} | |
} | |
func deleteSliceWithoutPreservingOrder(m []string) { | |
for i := len(m) - 1; i >= 0; i-- { | |
m[i], m = m[len(m)-1], m[:len(m)-1] | |
} | |
} | |
func stub(i string) string { | |
return i | |
} |
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
//Full version avaialble @ | |
//https://gist.github.com/Shaked/e46aa01741edf51dda6a/msa.go | |
package main | |
... | |
func main() { | |
deferRecoverPanicMap() | |
... | |
} | |
func deferRecoverPanicMap() { | |
m := runMap() | |
stub(m[40000]) | |
} | |
func runMap() map[int]string { | |
m := make(map[int]string) | |
for i := 0; i < amount; i++ { | |
m[i] = "someKey" | |
} | |
return m | |
} | |
func run... |
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
package main | |
import "testing" | |
var keys = randomKeys() | |
func BenchmarkDeferRecoverPanicMap(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
deferRecoverPanicMap() | |
} | |
} | |
func BenchmarkDeferRecoverPanicSliceExpandable(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
deferRecoverPanicSliceExpandable() | |
} | |
} | |
func BenchmarkDeferRecoverPanicSliceFixedSize(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
deferRecoverPanicSliceFixedSize() | |
} | |
} | |
func BenchmarkDeferRecoverPanicArray(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
deferRecoverPanicArray() | |
} | |
} | |
func BenchmarkRunMap(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
runMap() | |
} | |
} | |
func BenchmarkRunSliceExpandable(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
runSliceExpandable() | |
} | |
} | |
func BenchmarkRunSliceFixedSize(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
runSliceFixedSize() | |
} | |
} | |
func BenchmarkRunArray(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
runArray() | |
} | |
} | |
func BenchmarkSearchMap(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
searchMap(keys, runMap()) | |
} | |
} | |
func BenchmarkSearchSliceExpandable(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
searchSliceExpandable(keys, runSliceExpandable()) | |
} | |
} | |
func BenchmarkSearchSliceFixedSize(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
searchSliceFixedSize(keys, runSliceFixedSize()) | |
} | |
} | |
func BenchmarkSearchArray(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
searchArray(keys, runArray()) | |
} | |
} | |
func BenchmarkDeleteMap(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
deleteMap(runMap()) | |
} | |
} | |
func BenchmarkDeleteSlicePreservingOrder(b *testing.B) { | |
slice := runSliceFixedSize() | |
for n := 0; n < b.N; n++ { | |
deleteSlicePreservingOrder(slice) | |
} | |
} | |
func BenchmarkDeleteSliceWithoutPreservingOrder(b *testing.B) { | |
slice := runSliceFixedSize() | |
for n := 0; n < b.N; n++ { | |
deleteSliceWithoutPreservingOrder(slice) | |
} | |
} |
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
//Full version avaialble @ | |
//https://gist.github.com/Shaked/e46aa01741edf51dda6a/msa_test.go | |
... | |
func BenchmarkDeferRecoverPanicMap(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
deferRecoverPanicMap() | |
} | |
} | |
func Benchmark... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment