开发者

Understanding of CPU seconds

开发者 https://www.devze.com 2023-02-05 14:54 出处:网络
consider following example: import hotshot import hotshot.stats import time def test_sleep(): time.sleep(1)

consider following example:

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()

I got this output:

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)

I expect that I will have 0sec CPU time and 1sec wall time.

I expect开发者_JAVA百科 1 CPU second in case of busy loop, not in case of sleep.

Can someone explain why I get such results?

Thanks!


That sounds like a HotShot bug -- it's not getting CPU time from the OS, it's getting elapsed time (and maybe subtracting out I/O wait time, which would be zero in this case). If you do time python lol.py you'll see that you're not in a busy wait.

0

精彩评论

暂无评论...
验证码 换一张
取 消