Mutex vs RWMutex vs sync.Map, Concurrency map benchmark in different concurrent scenarios.
Check all the methods and code here benchmark/methods.go
Write
syncMap.Store(key, key)
Random Read
v, ok := syncMap.Load(key)
if !ok {
}
v = v
Range Read All
syncMap.Range(func(k, v interface{}) bool {
k = k
v = v
return true
})
Write
mutex.Lock()
mutexMap[key] = key
mutex.Unlock()
Random Read
mutex.Lock()
v, ok := mutexMap[key]
if !ok {
}
v = v
mutex.Unlock()
Range Read All
mutex.Lock()
for k, v := range mutexMap {
k = k
v = v
}
mutex.Unlock()
Write
rwMutex.Lock()
rwMutexMap[key] = key
rwMutex.Unlock()
Random Read
rwMutex.RLock()
v, ok := rwMutexMap[key]
if !ok {
}
v = v
rwMutex.RUnlock()
Range Read All
rwMutex.RLock()
for k, v := range rwMutexMap {
k = k
v = v
}
rwMutex.RUnlock()
Write
cMap.Set(key, key)
Random Read
v, ok := cMap.Get(key)
if !ok {
}
v = v
Range Read All
for i := range cMap.IterBuffered() {
i = i
}
Check all the test scenarios here benchmark/scenarios.go
Method | Test | Time |
---|---|---|
orcaman/concurrent-map | write (10 concurrents) (100000 iters) | 62.288221ms |
RWMutex | write (10 concurrents) (100000 iters) | 113.321432ms |
Mutex | write (10 concurrents) (100000 iters) | 121.2995ms |
sync.Map | write (10 concurrents) (100000 iters) | 203.216039ms |
Method | Test | Time |
---|---|---|
orcaman/concurrent-map | random_read (10 concurrents) (100000 iters) | 15.110771ms |
RWMutex | random_read (10 concurrents) (100000 iters) | 30.6894ms |
sync.Map | random_read (10 concurrents) (100000 iters) | 38.139147ms |
Mutex | random_read (10 concurrents) (100000 iters) | 85.278314ms |
Method | Test | Time |
---|---|---|
RWMutex | range_read (10 concurrents) (100 iters) | 143.054227ms |
Mutex | range_read (10 concurrents) (100 iters) | 905.993286ms |
sync.Map | range_read (10 concurrents) (100 iters) | 949.263514ms |
orcaman/concurrent-map | range_read (10 concurrents) (100 iters) | 2.366558255s |
Method | Test | Time |
---|---|---|
sync.Map | write (50 concurrents) (20000 iters) | 22.123396ms |
orcaman/concurrent-map | write (50 concurrents) (20000 iters) | 30.682173ms |
Mutex | write (50 concurrents) (20000 iters) | 122.591997ms |
RWMutex | write (50 concurrents) (20000 iters) | 134.832297ms |
Method | Test | Time |
---|---|---|
sync.Map | random_read (50 concurrents) (20000 iters) | 7.055942ms |
orcaman/concurrent-map | random_read (50 concurrents) (20000 iters) | 12.597161ms |
RWMutex | random_read (50 concurrents) (20000 iters) | 32.372341ms |
Mutex | random_read (50 concurrents) (20000 iters) | 118.862146ms |
Method | Test | Time |
---|---|---|
RWMutex | range_read (50 concurrents) (20 iters) | 104.010416ms |
sync.Map | range_read (50 concurrents) (20 iters) | 577.479455ms |
Mutex | range_read (50 concurrents) (20 iters) | 863.322488ms |
orcaman/concurrent-map | range_read (50 concurrents) (20 iters) | 1.715899163s |
Method | Test | Time |
---|---|---|
sync.Map | write (100 concurrents) (100000 iters) | 260.946224ms |
orcaman/concurrent-map | write (100 concurrents) (100000 iters) | 349.440739ms |
Mutex | write (100 concurrents) (100000 iters) | 1.630196524s |
RWMutex | write (100 concurrents) (100000 iters) | 1.680058588s |
Method | Test | Time |
---|---|---|
sync.Map | random_read (100 concurrents) (100000 iters) | 119.658652ms |
orcaman/concurrent-map | random_read (100 concurrents) (100000 iters) | 126.84391ms |
RWMutex | random_read (100 concurrents) (100000 iters) | 280.359023ms |
Mutex | random_read (100 concurrents) (100000 iters) | 1.644604438s |
Method | Test | Time |
---|---|---|
RWMutex | range_read (100 concurrents) (10 iters) | 95.519575ms |
sync.Map | range_read (100 concurrents) (10 iters) | 836.398188ms |
Mutex | range_read (100 concurrents) (10 iters) | 972.755566ms |
orcaman/concurrent-map | range_read (100 concurrents) (10 iters) | 2.795446594s |