Last active
December 20, 2015 14:39
-
-
Save aybabtme/6147846 to your computer and use it in GitHub Desktop.
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 ( | |
"strconv" | |
"testing" | |
) | |
func Benchmark_LookupBeforeWrite_FullMap(b *testing.B) { | |
testMap := fillWholeMap(b.N) | |
keys := generateKeyList(b.N) | |
values := generateValueList(b.N) | |
var key string | |
var value string | |
b.StartTimer() | |
for n := 0; n < b.N; n++ { | |
key = keys[n] | |
value = values[n] | |
if _, ok := testMap[key]; !ok { | |
testMap[key] = value | |
} | |
} | |
b.StopTimer() | |
} | |
func Benchmark_LookupBeforeWrite_HalfFullMap(b *testing.B) { | |
testMap := fillMapForEvenValues(b.N) | |
keys := generateKeyList(b.N) | |
values := generateValueList(b.N) | |
var key string | |
var value string | |
b.StartTimer() | |
for n := 0; n < b.N; n++ { | |
key = keys[n] | |
value = values[n] | |
if _, ok := testMap[key]; !ok { | |
testMap[key] = value | |
} | |
} | |
b.StopTimer() | |
} | |
func Benchmark_LookupBeforeWrite_EmptyMap(b *testing.B) { | |
testMap := make(map[string]string) | |
keys := generateKeyList(b.N) | |
values := generateValueList(b.N) | |
var key string | |
var value string | |
b.StartTimer() | |
for n := 0; n < b.N; n++ { | |
key = keys[n] | |
value = values[n] | |
if _, ok := testMap[key]; !ok { | |
testMap[key] = value | |
} | |
} | |
b.StopTimer() | |
} | |
func Benchmark_JustWrite_FullMap(b *testing.B) { | |
testMap := fillWholeMap(b.N) | |
keys := generateKeyList(b.N) | |
values := generateValueList(b.N) | |
var key string | |
var value string | |
b.StartTimer() | |
for n := 0; n < b.N; n++ { | |
key = keys[n] | |
value = values[n] | |
testMap[key] = value | |
} | |
b.StopTimer() | |
} | |
func Benchmark_JustWrite_HalfFullMap(b *testing.B) { | |
testMap := fillMapForEvenValues(b.N) | |
keys := generateKeyList(b.N) | |
values := generateValueList(b.N) | |
var key string | |
var value string | |
b.StartTimer() | |
for n := 0; n < b.N; n++ { | |
key = keys[n] | |
value = values[n] | |
testMap[key] = value | |
} | |
b.StopTimer() | |
} | |
func Benchmark_JustWrite_EmptyMap(b *testing.B) { | |
testMap := make(map[string]string) | |
keys := generateKeyList(b.N) | |
values := generateValueList(b.N) | |
var key string | |
var value string | |
b.StartTimer() | |
for n := 0; n < b.N; n++ { | |
key = keys[n] | |
value = values[n] | |
testMap[key] = value | |
} | |
b.StopTimer() | |
} | |
func fillWholeMap(n int) map[string]string { | |
output := make(map[string]string) | |
for i := 0; i < n; i++ { | |
output["key"+strconv.Itoa(i)] = "value" + strconv.Itoa(i) | |
} | |
return output | |
} | |
func fillMapForEvenValues(n int) map[string]string { | |
output := make(map[string]string) | |
for i := 0; i < n; i++ { | |
if i%2 == 0 { | |
output["key"+strconv.Itoa(i)] = "value" + strconv.Itoa(i) | |
} | |
} | |
return output | |
} | |
func generateKeyList(n int) []string { | |
output := make([]string, n) | |
for i := 0; i < n; i++ { | |
output[i] = "key" + strconv.Itoa(i) | |
} | |
return output | |
} | |
func generateValueList(n int) []string { | |
output := make([]string, n) | |
for i := 0; i < n; i++ { | |
output[i] = "value" + strconv.Itoa(i) | |
} | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment