2011-01-20 44 views
3

考虑下面的例子:谅解CPU秒

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第二,不睡眠的情况下。

有人可以解释为什么我会得到这样的结果吗?

谢谢!

回答

1

这听起来像一个自命不凡的错误 - 它不是从OS获得CPU时间,它变得经过时间(也许减去I/O等待时间,这将是在这种情况下,零)。如果你这样做time python lol.py你会发现你并不是在忙着等待。

+1

是否记录在某处?我用cProfile得到了同样的结果。 “时间python.lol”工作正常。 – lstipakov 2011-01-20 20:57:56