Last active
September 10, 2017 03:48
-
-
Save deckarep/6fb7c4ab1b33966629e832dab009c697 to your computer and use it in GitHub Desktop.
Benchmark of sync.Map vs builtin map with Load operations
This file contains hidden or 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
func BenchmarkLoadRegularFound(b *testing.B) { | |
nums := nrand(b.N) | |
rm := NewRegularIntMap() | |
for _, v := range nums { | |
rm.Store(v, v) | |
} | |
currentResult := 0 | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
currentResult, _ = rm.Load(nums[i]) | |
} | |
globalResult = currentResult | |
} | |
func BenchmarkLoadRegularNotFound(b *testing.B) { | |
nums := nrand(b.N) | |
rm := NewRegularIntMap() | |
for _, v := range nums { | |
rm.Store(v, v) | |
} | |
currentResult := 0 | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
currentResult, _ = rm.Load(i) | |
} | |
globalResult = currentResult | |
} | |
func BenchmarkLoadSyncFound(b *testing.B) { | |
nums := nrand(b.N) | |
var sm sync.Map | |
for _, v := range nums { | |
sm.Store(v, v) | |
} | |
currentResult := 0 | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
r, ok := sm.Load(nums[i]) | |
if ok { | |
currentResult = r.(int) | |
} | |
} | |
globalResult = currentResult | |
} | |
func BenchmarkLoadSyncNotFound(b *testing.B) { | |
nums := nrand(b.N) | |
var sm sync.Map | |
for _, v := range nums { | |
sm.Store(v, v) | |
} | |
currentResult := 0 | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
r, ok := sm.Load(i) | |
if ok { | |
currentResult = r.(int) | |
} | |
} | |
globalResult = currentResult | |
} | |
/* | |
BenchmarkLoadRegularFound-32 10000000 180 ns/op | |
BenchmarkLoadRegularNotFound-32 20000000 107 ns/op | |
BenchmarkLoadSyncFound-32 10000000 200 ns/op | |
BenchmarkLoadSyncNotFound-32 20000000 291 ns/op | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment