Created
October 25, 2012 03:28
-
-
Save border/3950259 to your computer and use it in GitHub Desktop.
Golang mutex and RWMutex 测试比较
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
~ ✗ diff -ruN mutex.go mutexv2.go | |
--- mutex.go 2012-10-25 11:30:04.170656858 +0800 | |
+++ mutexv2.go 2012-10-25 11:30:04.174656858 +0800 | |
@@ -9,8 +9,9 @@ | |
) | |
type Msg struct { | |
- mutex sync.Mutex | |
- count map[int64]string | |
+ rmutex sync.RWMutex | |
+ mutex sync.Mutex | |
+ count map[int64]string | |
} | |
func (m *Msg) InitMsg(key int64, value string) { | |
@@ -21,15 +22,15 @@ | |
func (m *Msg) Get(key int64, c chan bool) (str string) { | |
defer func() { <-c }() | |
- m.mutex.Lock() | |
+ m.rmutex.RLock() | |
if _, ok := m.count[key]; !ok { | |
- m.mutex.Unlock() | |
+ m.rmutex.RUnlock() | |
m.InitMsg(key, strconv.Itoa(int(key))) | |
} else { | |
- m.mutex.Unlock() | |
+ m.rmutex.RUnlock() | |
} | |
- m.mutex.Lock() | |
- defer m.mutex.Unlock() | |
+ m.rmutex.RLock() | |
+ defer m.rmutex.RUnlock() | |
if str, ok := m.count[key]; ok { | |
return str | |
} |
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 ( | |
"fmt" | |
"runtime" | |
"strconv" | |
"sync" | |
"time" | |
) | |
type Msg struct { | |
mutex sync.Mutex | |
count map[int64]string | |
} | |
func (m *Msg) InitMsg(key int64, value string) { | |
m.mutex.Lock() | |
defer m.mutex.Unlock() | |
m.count[key] = value | |
} | |
func (m *Msg) Get(key int64, c chan bool) (str string) { | |
defer func() { <-c }() | |
m.mutex.Lock() | |
if _, ok := m.count[key]; !ok { | |
m.mutex.Unlock() | |
m.InitMsg(key, strconv.Itoa(int(key))) | |
} else { | |
m.mutex.Unlock() | |
} | |
m.mutex.Lock() | |
defer m.mutex.Unlock() | |
if str, ok := m.count[key]; ok { | |
return str | |
} | |
return | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU() * 2) | |
msg := &Msg{count: map[int64]string{}} | |
var i int64 | |
c := make(chan bool, 2000) | |
t1 := time.Now().UnixNano() | |
for i = 0; i <= 1024000; i++ { | |
msg.InitMsg(i, strconv.Itoa(int(i))) | |
} | |
for i = 0; i <= 2048000; i++ { | |
c <- true | |
go msg.Get(i, c) | |
} | |
t2 := time.Now().UnixNano() | |
fmt.Println(t2 - t1) | |
} |
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 ( | |
"fmt" | |
"runtime" | |
"strconv" | |
"sync" | |
"time" | |
) | |
type Msg struct { | |
rmutex sync.RWMutex | |
mutex sync.Mutex | |
count map[int64]string | |
} | |
func (m *Msg) InitMsg(key int64, value string) { | |
m.mutex.Lock() | |
defer m.mutex.Unlock() | |
m.count[key] = value | |
} | |
func (m *Msg) Get(key int64, c chan bool) (str string) { | |
defer func() { <-c }() | |
m.rmutex.RLock() | |
if _, ok := m.count[key]; !ok { | |
m.rmutex.RUnlock() | |
m.InitMsg(key, strconv.Itoa(int(key))) | |
} else { | |
m.rmutex.RUnlock() | |
} | |
m.rmutex.RLock() | |
defer m.rmutex.RUnlock() | |
if str, ok := m.count[key]; ok { | |
return str | |
} | |
return | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU() * 2) | |
msg := &Msg{count: map[int64]string{}} | |
var i int64 | |
c := make(chan bool, 2000) | |
t1 := time.Now().UnixNano() | |
for i = 0; i <= 1024000; i++ { | |
msg.InitMsg(i, strconv.Itoa(int(i))) | |
} | |
for i = 0; i <= 2048000; i++ { | |
c <- true | |
go msg.Get(i, c) | |
} | |
t2 := time.Now().UnixNano() | |
fmt.Println(t2 - t1) | |
} |
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
~ ✗ go run mutex.go | |
7566709000 | |
~ ✗ go run mutexv2.go | |
3387533000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
will throw
in the go version go1.8 darwin/amd64