【问题标题】:Understanding of CPU secondsCPU秒数的理解
【发布时间】:2011-06-12 16:48:07
【问题描述】:

考虑以下示例:

import hotshot
import hotshot.stats
import time

def test_sleep():
    time.sleep(1)

def main():
    prof = hotshot.Profile("lol.prof")
    prof.runcall(test_sleep)
    prof.close()

    stats = hotshot.stats.load("lol.prof")
    stats.sort_stats("time", "calls")
    stats.print_stats(20)

if __name__ == "__main__":
    main()

我得到了这个输出:

debian:# python lol.py
         1 function calls in 1.000 CPU seconds

   Ordered by: internal time, call count

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    1.000    1.000    1.000    1.000 lol.py:6(test_sleep)
        0    0.000             0.000          profile:0(profiler)

我希望我有 0 秒的 CPU 时间和 1 秒的挂墙时间。

我希望在繁忙循环的情况下需要 1 个 CPU 秒,而不是在睡眠的情况下。

谁能解释我为什么会得到这样的结果?

谢谢!

【问题讨论】:

    标签: python profile cpu-usage


    【解决方案1】:

    这听起来像是一个 HotShot 错误——它没有从操作系统获取 CPU 时间,而是获取了经过的时间(并且可能减去了 I/O 等待时间,在这种情况下为零)。如果你这样做time python lol.py,你会发现你并没有在忙着等待。

    【讨论】:

    • 它是否记录在某处?我用 cProfile 得到了相同的结果。 “时间 python.lol”工作正常。
    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 2021-06-25
    相关资源
    最近更新 更多