func Benchmark_foreach1(b *testing.B) {
var test map[int]int
test = make(map[int]int)
for i := 0; i < 100000; i++ {
test[i] = 1
}
for i := 0; i < b.N; i++ {
for i, _ := range test {
if test[i] != 1 {
panic("ds")
}
}
}
}
func Benchmark_foreach2(b *testing.B) {
var test map[int]int
test = make(map[int]int)
for i := 0; i < 100000; i++ {
test[i] = 1
}
for i := 0; i < b.N; i++ …Run Code Online (Sandbox Code Playgroud) 在golang中,sync.Mutex Lock和Unlock是usul操作,但是Lock和defer Unlock的正确顺序是什么?
mu.Lock()
defer mu.Unlock()
Run Code Online (Sandbox Code Playgroud)
要么
defer mu.Unlock()
mu.Lock()
Run Code Online (Sandbox Code Playgroud)
哪个最好?