1 回答

TA貢獻1805條經驗 獲得超10個贊
首先,我確認這個問題中的 Go 程序確實比 C++ 程序運行得明顯慢。我認為知道原因確實很有趣。
我用 分析了 Go 客戶端和服務器,pprof
發現這syscall.Syscall
占用了總執行時間的 70%。根據這張票,在 Go 中系統調用比在 C 中慢大約 1.4 倍。
(pprof) top -cum
Showing nodes accounting for 18.78s, 67.97% of 27.63s total
Dropped 44 nodes (cum <= 0.14s)
Showing top 10 nodes out of 44
? flat? flat%? ?sum%? ? ? ? cum? ?cum%
?0.11s? ?0.4%? ?0.4%? ? ?22.65s 81.98%? main.main
? ? ?0? ? ?0%? ?0.4%? ? ?22.65s 81.98%? runtime.main
18.14s 65.65% 66.05%? ? ?19.91s 72.06%? syscall.Syscall
?0.03s? 0.11% 66.16%? ? ?12.91s 46.72%? net.(*conn).Read
?0.10s? 0.36% 66.52%? ? ?12.88s 46.62%? net.(*netFD).Read
?0.16s? 0.58% 67.10%? ? ?12.78s 46.25%? internal/poll.(*FD).Read
?0.06s? 0.22% 67.32%? ? ?11.87s 42.96%? syscall.Read
?0.11s? ?0.4% 67.72%? ? ?11.81s 42.74%? syscall.read
?0.02s 0.072% 67.79%? ? ? 9.30s 33.66%? net.(*conn).Write
?0.05s? 0.18% 67.97%? ? ? 9.28s 33.59%? net.(*netFD).Write
Conn.Write我逐漸減少和調用的次數Conn.Read并相應地增加緩沖區的大小,以便傳輸的字節數保持不變。結果是程序進行的這些調用越少,其性能就越接近 C++ 版本。
- 1 回答
- 0 關注
- 158 瀏覽
添加回答
舉報