FreeBSD上的Python时钟功能 [英] Python clock function on FreeBSD

查看:118
本文介绍了FreeBSD上的Python时钟功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FreeBSD上测试Pythons的time.clock()函数时,我注意到它总是返回相同的值,约为0.156

time.time()函数可以正常工作,但我需要分辨率稍高的东西.

是否有人绑定了C函数以及是否有其他高分辨率计时器?

我没有进行概要分析,因此TimeIt模块在这里并不是很合适.

解决方案

time.clock()返回处理器时间.也就是说,当前进程已在处理器上使用了多少时间.因此,如果您有一个名为"clock.py"的Python脚本,它会执行import time;print time.clock(),实际上每次运行时都会打印出几乎完全相同的内容,因为每次都会启动一个新进程.

以下是python控制台日志,可能会向您解释:

>>> import time
>>> time.clock()
0.11
>>> time.clock()
0.11
>>> time.clock()
0.11
>>> for x in xrange(100000000): pass
... 
>>> time.clock()
7.7800000000000002
>>> time.clock()
7.7800000000000002
>>> time.clock()
7.7800000000000002

我希望这可以弄清楚一切.

While testing Pythons time.clock() function on FreeBSD I've noticed it always returns the same value, around 0.156

The time.time() function works properly but I need a something with a slightly higher resolution.

Does anyone the C function it's bound to and if there is an alternative high resolution timer?

I'm not profiling so the TimeIt module is not really appropriate here.

解决方案

time.clock() returns the processor time. That is, how much time the current process has used on the processor. So if you have a Python script called "clock.py", that does import time;print time.clock() it will indeed print about exactly the same each time you run it, as a new process is started each time.

Here is a python console log that might explain it to you:

>>> import time
>>> time.clock()
0.11
>>> time.clock()
0.11
>>> time.clock()
0.11
>>> for x in xrange(100000000): pass
... 
>>> time.clock()
7.7800000000000002
>>> time.clock()
7.7800000000000002
>>> time.clock()
7.7800000000000002

I hope this clarifies things.

这篇关于FreeBSD上的Python时钟功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆