第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對通道/互斥內(nèi)存消耗/分配進(jìn)行基準(zhǔn)測試?

如何對通道/互斥內(nèi)存消耗/分配進(jìn)行基準(zhǔn)測試?

Go
猛跑小豬 2022-05-10 15:54:23
我嘗試比較使用通道獲取一個值與使用互斥鎖。頻道示例:func BenchmarkNewDummy(b *testing.B) {    one := make(chan string, 1)    two := make(chan string, 1)    var wg sync.WaitGroup    wg.Add(2)    go doWork(&wg, one)    go doWork(&wg, two)    wg.Wait()    fmt.Println(<-one)    fmt.Println(<-two)}func doWork(wg *sync.WaitGroup, one chan string) {    defer wg.Done()    one <- "hell0"}命令:go test -bench=. -benchmem -run BenchmarkNewDummy -cpuprofile cpuCh.out -memprofile memCh.prof 輸出沒有提供任何有用的信息goos: darwingoarch: amd64BenchmarkNewDummy-8     hell0hell0hell0hell0hell0hell0hell0hell0hell0hell02000000000               0.00 ns/op            0 B/op          0 allocs/opPASSok        0.508s與互斥量情況幾乎相同:func BenchmarkNewDummy(b *testing.B) {    one := ""    two := ""    var wg sync.WaitGroup    wg.Add(2)    var mu sync.Mutex    go func() {        mu.Lock()        defer mu.Unlock()        defer wg.Done()        one = "hello"    }()    go func() {        mu.Lock()        defer mu.Unlock()        defer wg.Done()        two = "hello"    }()    wg.Wait()    fmt.Println(one)    fmt.Println(two)}輸出:goos: darwingoarch: BenchmarkNewDummy-8     hellohellohellohellohellohellohellohellohellohello2000000000               0.00 ns/op            0 B/op          0 allocs/opPASSok        0.521s內(nèi)存圖看起來幾乎相同,但使用 mutext 更大的內(nèi)存分配,但也沒有提供信息:是否可以比較通道和互斥體內(nèi)存消耗?
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻(xiàn)1815條經(jīng)驗 獲得超6個贊

你做的基準(zhǔn)測試是錯誤的。引用包文檔testing:


示例基準(zhǔn)函數(shù)如下所示:


func BenchmarkHello(b *testing.B) {

    for i := 0; i < b.N; i++ {

        fmt.Sprintf("hello")

    }

}

基準(zhǔn)函數(shù)必須運行目標(biāo)代碼 bN 次。 在基準(zhǔn)執(zhí)行期間,調(diào)整 bN 直到基準(zhǔn)函數(shù)持續(xù)足夠長的時間以可靠地計時。


也不要fmt.PrintXX()在基準(zhǔn)代碼中包含調(diào)用,您會扭曲結(jié)果。


而是對這些函數(shù)進(jìn)行基準(zhǔn)測試:


func newDummy() {

    one := make(chan string, 1)

    two := make(chan string, 1)

    var wg sync.WaitGroup

    wg.Add(2)

    go doWork(&wg, one)

    go doWork(&wg, two)

    wg.Wait()

    <-one

    <-two

}


func doWork(wg *sync.WaitGroup, one chan string) {

    defer wg.Done()

    one <- "hell0"

}


func newDummy2() {

    one, two := "", ""

    var wg sync.WaitGroup

    wg.Add(2)

    var mu sync.Mutex

    go func() {

        mu.Lock()

        defer mu.Unlock()

        defer wg.Done()

        one = "hello"

    }()

    go func() {

        mu.Lock()

        defer mu.Unlock()

        defer wg.Done()

        two = "hello"

    }()

    wg.Wait()

    _, _ = one, two

}

像這樣:


func BenchmarkNewDummy(b *testing.B) {

    for i := 0; i < b.N; i++ {

        newDummy()

    }

}


func BenchmarkNewDummy2(b *testing.B) {

    for i := 0; i < b.N; i++ {

        newDummy2()

    }

}

對其進(jìn)行基準(zhǔn)測試:


go test -bench . -benchmem

我得到這樣的輸出:


BenchmarkNewDummy-4    605662      1976 ns/op     240 B/op      5 allocs/op

BenchmarkNewDummy2-4   927031      1627 ns/op      56 B/op      4 allocs/op

從結(jié)果來看,newDummy()平均執(zhí)行 5 次分配,總計 250 字節(jié)。newDummy2()執(zhí)行 4 次分配,總共 56 個字節(jié)。


查看完整回答
反對 回復(fù) 2022-05-10
  • 1 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號