gpt4 book ai didi

Go 分析器 (pprof) 时间差异

转载 作者:IT王子 更新时间:2023-10-29 01:38:47 27 4
gpt4 key购买 nike

当我使用 Linux 的 time 实用程序测量我的 Go 程序的运行时间时,我得到以下输出:

real    1m9.177s
user 7m1.788s
sys 0m39.016s

当我在 Go 的 pprof CPU 分析器中查看完全相同的程序执行的输出时,我得到以下输出:

Entering interactive mode (type "help" for commands)
(pprof) top
143.32s of 176s total (81.43%)

pprof 从哪里得到这个 176s 的数字?它既不是时钟时间也不是 CPU 时间。我在 GOMAXPROCS 设置为 8 的情况下运行这个程序,我觉得这与它有关。 pprof 究竟如何测量运行时间,为什么它与 linux 的 time 不同?

最佳答案

time 实用程序和pprof 工具以不同的方式运行,应该以不同的方式使用。不要试图调和它们。 time 测量接近于程序的准确时间; pprof 测量函数和方法的相对时间。 pprof 是一个侵入式统计样本。启用 CPU 分析后,Go 程序每秒停止大约 100 次,并在当前执行的 goroutine 堆栈上记录一个由程序计数器组成的样本。抽样是有成本的,结果是对实际人口的估计。

使用time来衡量程序整体的实际CPU时间,即绝对时间。使用 pprof 使用相对时间百分比来估计大部分时间花费在函数和方法中的位置。 pprof 识别瓶颈。对于实际 CPU 时间,请使用 Go testing 包基准测试。

有关示例,请参阅 Profiling Go Programs :

By using Go's profiling tools to identify and correct specific bottlenecks, we can make the Go loop finding program run an order of magnitude faster and use 6x less memory.

关于Go 分析器 (pprof) 时间差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42406702/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com