我正在使用具有 8 個內(nèi)核的機器(具有“2,8 GHz Intel Core i7”處理器的 Mac),我可以看到正在運行fmt.Println(runtime.NumCPU()).我已經(jīng)實現(xiàn)了一個非常簡單的工作池模型來同時處理一些進入池的請求。進程類型是“CPU 密集型”,我想感受一下在給 GO 更多內(nèi)核時性能會提高多少。所以代碼如下func Run(poolSize int, workSize int, loopSize int, maxCores int) { runtime.GOMAXPROCS(maxCores) var wg sync.WaitGroup wg.Add(poolSize) defer wg.Wait() // this is the channel where we write the requests for work to be performed by the pool workStream := make(chan int) // cpuIntensiveWork simulates an CPU intensive process var cpuIntensiveWork = func(input int) { res := input for i := 0; i < loopSize; i++ { res = res + i } } // worker is the function that gets fired by the pool worker := func(wg *sync.WaitGroup, workStream chan int, id int) { defer wg.Done() for req := range workStream { cpuIntensiveWork(req) } } // launch the goroutines of the pool for i := 0; i < poolSize; i++ { go worker(&wg, workStream, i) } // feed the workStream until the end and then close the channel for workItemNo := 0; workItemNo < workSize; workItemNo++ { workStream <- workItemNo } close(workStream)}基準是這些var numberOfWorkers = 100var numberOfRequests = 1000var loopSize = 100000func Benchmark_1Core(b *testing.B) { for i := 0; i < b.N; i++ { Run(numberOfWorkers, numberOfRequests, loopSize, 1) }}func Benchmark_2Cores(b *testing.B) { for i := 0; i < b.N; i++ { Run(numberOfWorkers, numberOfRequests, loopSize, 2) }}func Benchmark_4Cores(b *testing.B) { for i := 0; i < b.N; i++ { Run(numberOfWorkers, numberOfRequests, loopSize, 4) }}func Benchmark_8Cores(b *testing.B) { for i := 0; i < b.N; i++ { Run(numberOfWorkers, numberOfRequests, loopSize, 8) }}運行基準測試我注意到,從 1 核到 2 核再到 4 核,性能幾乎呈線性增長。但是我從 4 核到 8 核的性能差異非常有限。這是預期的行為嗎?如果是這樣,根本原因是什么?
1 回答

一只名叫tom的貓
TA貢獻1906條經(jīng)驗 獲得超3個贊
有了多核,事情就會變得有趣。最可能的解釋是您沒有八個內(nèi)核,而是四個具有超線程的內(nèi)核,這會給您帶來更少的加速 - 有時根本沒有。
要檢查的另一個可能的解釋是每個線程都使用大量內(nèi)存,并且您的緩存內(nèi)存不足。或者你達到了內(nèi)存帶寬飽和的地步,此時沒有多少處理器可以幫助你。
- 1 回答
- 0 關(guān)注
- 146 瀏覽
添加回答
舉報
0/150
提交
取消