gpt4 book ai didi

macos - 测量 OS X 中耗时

转载 作者:行者123 更新时间:2023-12-03 17:02:14 24 4
gpt4 key购买 nike

我需要测量耗时,以便知道何时超过了某个时间段。

我曾经使用 Ticks()Microseconds() 来实现此目的,但这两个函数现在均已弃用。

CFAbsoluteTimeGetCurrent 不是正确的使用方法,因为它可能会向后运行,如文档中所述:

Repeated calls to this function do not guarantee monotonically increasing results. The system time may decrease due to synchronization with external time references or due to an explicit user change of the clock.

还有什么没有被弃用并且相当面向 future 的?

最佳答案

一种方法,如 Q&A 1398 中所述,就是使用mach_absolute_time,如下所示:

    static mach_timebase_info_data_t sTimebaseInfo;
mach_timebase_info(&sTimebaseInfo); // Determines the time scale

uint64_t t1 = mach_absolute_time();
...
uint64_t t2 = mach_absolute_time();
uint64_t elapsedNano = (t2-t1) * sTimebaseInfo.numer / sTimebaseInfo.denom;

不过,这也可能不是万无一失的。在某些情况下,这些值可能会溢出,如 this answer 中指出的。 .

关于macos - 测量 OS X 中耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30199410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com