适用于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中的mach_absolute_time()。 h header。
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屋!