在Python中获取mach_absolute_time/UpTime()(以纳秒为单位)

发布于 2024-08-13 20:29:07 字数 482 浏览 3 评论 0原文

我需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

非要怀念 2024-08-20 20:29:07

CGEventTimestamp to NSDate 的答案之一的代码中,我发现 -[NSProcessInfo systemUptime],可用从 10.6 开始。这给了我以十进制秒为单位的时间,我可以将其相乘:

from Foundation import *
NSProcessInfo.processInfo().systemUptime() * 1e9

结果确实具有纳秒精度,并且应该可以很好地满足我的需要。

更新:以下方法也适用,并且也兼容 10.5:

from Quartz.QuartzCore import *
CACurrentMediaTime() * 1e9

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:

from Foundation import *
NSProcessInfo.processInfo().systemUptime() * 1e9

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:

from Quartz.QuartzCore import *
CACurrentMediaTime() * 1e9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文