iPhone 的高分辨率计时器? [英] High-resolution timer for iPhone?
问题描述
我正在寻找 iPhone 的高分辨率计时代码,以便进行一些性能计时.我想写这样的代码:
I'm looking for high-resolution timing code for iPhone, in order to do some performance timings. I'd like to write code like this:
HighResolutionTimer* myTimer = [[HighResolutionTimer alloc]init];
[myTimer start];
[self doSomeLengthyOperation];
NSLog( @"doSomeLengthyOperation took %f seconds", [myTimer elapsedTime] );
推荐答案
查看 mach/mach_time.h 头文件中的 mach_absolute_time().
Look into mach_absolute_time() in the mach/mach_time.h header.
不要使用 NSDate.当 ntp 做它的事情时,NSDate 甚至不能保证偶尔不会倒退.
Don't use NSDate. NSDate isn't even guaranteed to not go backwards occasionally, when ntp does its thing.
(设备可能有时钟漂移.如果iOS设备快速漂移几秒钟,那么当NTP纠正这个漂移时,你会看到时钟突然倒退几秒钟.非常不利于计时.mach_time使用一个计数器永远不会被 NTP 纠正,因此不能倒退,因此在时间上要好得多.)
(Devices can have clock drift. If the iOS device drifts fast a few seconds, then when NTP corrects this drift, you will see the clock suddenly go backwards a few seconds. Very bad for timing use. mach_time uses a counter that doesn't ever get corrected by NTP, thus can't go backwards, thus is far better for timing.)
这篇关于iPhone 的高分辨率计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!