在Python中获取mach_absolute_time/UpTime()(以纳秒为单位)
我需要从 Mac OS X 10.6 上运行的 Python 程序访问启动以来经过的时间(以纳秒为单位)。
我使用以下 Carbon 调用在 C 代码中获取此内容:
AbsoluteTime uptimeAbs = AbsoluteToNanoseconds(UpTime());
uint64_t elapsedTime = ((uint64_t)uptimeAbs.hi << 32) + uptimeAbs.lo;
Is it possible to get to those functions in Python using a Carbon or PyObjC module?我尝试过:
from Carbon import *
UpTime()
但得到“NameError:名称'UpTime'未定义”。
如何从 OS X 上的 Python 访问该值?
I need to access the elapsed time since startup in nanoseconds from a Python program running on Mac OS X 10.6.
I use the following Carbon calls to get this in C code:
AbsoluteTime uptimeAbs = AbsoluteToNanoseconds(UpTime());
uint64_t elapsedTime = ((uint64_t)uptimeAbs.hi << 32) + uptimeAbs.lo;
Is it possible to get to these functions in Python using a Carbon or PyObjC module? I tried:
from Carbon import *
UpTime()
but got "NameError: name 'UpTime' is not defined".
How can I get access to this value from Python on OS X?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 CGEventTimestamp to NSDate 的答案之一的代码中,我发现 -[NSProcessInfo systemUptime],可用从 10.6 开始。这给了我以十进制秒为单位的时间,我可以将其相乘:
结果确实具有纳秒精度,并且应该可以很好地满足我的需要。
更新:以下方法也适用,并且也兼容 10.5:
Within the code in one of the answers at CGEventTimestamp to NSDate, I found -[NSProcessInfo systemUptime], available starting in 10.6. This gives me the time in decimal seconds, which I can multiply:
The result does have nanosecond precision, and should work nicely for my needs.
Update: the following method also works, and is compatible with 10.5 as well: