V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
theknotyouknow
V2EX  ›  Go 编程语言

为啥读写锁和互斥锁效率没看出来差别啊,囧

  •  
  •   theknotyouknow · 2021-09-27 16:21:08 +08:00 · 1696 次点击
    这是一个创建于 1145 天前的主题,其中的信息可能已经有所发展或是发生改变。
    package main`
    
    import (
    	"sync"
    	"testing"
    	"time"
    )
    
    const (
    	cost = 10 * time.Microsecond
    )
    
    type RW interface {
    	Write()
    	Read()
    }
    
    type Lock struct {
    	count int
    	mu    sync.Mutex
    }
    
    func (l *Lock) Read() {
    	l.mu.Lock()
    	time.Sleep(cost)
    	_ = l.count
    	l.mu.Unlock()
    }
    
    func (l *Lock) Write() {
    	l.mu.Lock()
    	l.count++
    	time.Sleep(cost)
    	l.mu.Unlock()
    }
    
    type RWLock struct {
    	count int
    	mu    sync.RWMutex
    }
    
    func (r *RWLock) Read() {
    	r.mu.Lock()
    	time.Sleep(cost)
    	_ = r.count
    	r.mu.Unlock()
    }
    
    func (r *RWLock) Write() {
    	r.mu.Lock()
    	r.count++
    	time.Sleep(cost)
    	r.mu.Unlock()
    }
    
    func benchmark(b *testing.B, rw RW, read, write int) {
    
    	for i := 0; i < b.N; i++ {
    		var wg sync.WaitGroup
    		for k := 0; k < read*100; k++ {
    			wg.Add(1)
    			go func() {
    				rw.Read()
    				wg.Done()
    			}()
    		}
    
    		for m := 0; m < write*100; m++ {
    			wg.Add(1)
    			go func() {
    				rw.Write()
    				wg.Done()
    			}()
    		}
    		wg.Wait()
    	}
    
    }
    
    func BenchmarkReadMore(b *testing.B) {
    	benchmark(b, &Lock{}, 9, 1)
    }
    
    func BenchmarkReadMoreRW(b *testing.B) {
    	benchmark(b, &RWLock{}, 9, 1)
    }
    
    func BenchmarkWriteMore(b *testing.B) {
    	benchmark(b, &Lock{}, 1, 9)
    }
    
    func BenchmarkWriteMoreRW(b *testing.B) {
    	benchmark(b, &RWLock{}, 1, 9)
    }
    
    func BenchmarkReadEqual(b *testing.B) {
    	benchmark(b, &Lock{}, 5, 5)
    }
    
    func BenchmarkReadEqualRW(b *testing.B) {
    	benchmark(b, &RWLock{}, 5, 5)
    }
    

    下面这是我的执行结果:

    goarch: amd64
    pkg: test
    cpu: Intel(R) Core(TM) i5-8257U CPU @ 1.40GHz
    |BenchmarkReadMore-8| 62| 18909825| ns/op|
    |BenchmarkReadMoreRW-8 | 63| 18825713 |ns/op | |BenchmarkWriteMore-8 | 63 | 18774136| ns/op | |BenchmarkWriteMoreRW-8 | 63 | 20889956 ns/op | PASS
    ok test 5.407s

    3 条回复    2021-09-27 16:31:44 +08:00
    lcdtyph
        1
    lcdtyph  
       2021-09-27 16:27:11 +08:00 via iPhone
    RLock, RUnlock
    lcdtyph
        2
    lcdtyph  
       2021-09-27 16:30:10 +08:00 via iPhone
    你加的全是写锁,没有读锁
    theknotyouknow
        3
    theknotyouknow  
    OP
       2021-09-27 16:31:44 +08:00
    @lcdtyph 感谢感谢~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2787 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 13:07 · PVG 21:07 · LAX 05:07 · JFK 08:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.