iPhone 的高分辨率计时器?

发布于 2024-09-15 14:59:14 字数 265 浏览 3 评论 0 原文

我正在寻找 iPhone 的高分辨率计时代码,以便进行一些性能计时。我想写这样的代码:

HighResolutionTimer* myTimer = [[HighResolutionTimer alloc]init];
[myTimer start];
[self doSomeLengthyOperation];
NSLog( @"doSomeLengthyOperation took %f seconds", [myTimer elapsedTime] );

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] );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

时常饿 2024-09-22 14:59:14

查看 mach/mach_time.h 标头中的 mach_absolute_time() 。

不要使用 NSDate。当 ntp 执行其操作时,NSDate 甚至不能保证偶尔不会倒退。

(设备可能会出现时钟漂移。如果 iOS 设备快速漂移几秒钟,那么当 NTP 纠正这种漂移时,您会看到时钟突然向后倒退几秒钟。对于计时使用来说非常糟糕。mach_time 使用的计数器不曾经被 NTP 纠正过,因此不能倒退,因此对于计时来说要好得多。)

Look into mach_absolute_time() in the mach/mach_time.h header.

Don't use NSDate. NSDate isn't even guaranteed to not go backwards occasionally, when ntp does its thing.

(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.)

ぃ弥猫深巷。 2024-09-22 14:59:14

更好的选择是 CACurrentMediaTime() 使用 mach_absolute_time() 但将其转换为 CFTimeInterval(即以秒为单位的双精度时间)。

A better option is CACurrentMediaTime() which uses mach_absolute_time() but converts it to a CFTimeInterval (i.e., time in seconds as a double) for you.

心清如水 2024-09-22 14:59:14

这是我对使用 mach_absolute_time() 的时钟计时器的回答,基于计算方法 此处显示,以及 NSDate。就准确度而言,它们实际上是相同的。

Mach 版本

double machGetClockS()
{
  static bool init = 0 ;
  static mach_timebase_info_data_t tbInfo ;
  static double conversionFactor ;
  if(!init)
  {
    init = 1 ;
    // get the time base
    mach_timebase_info( &tbInfo ) ;
    conversionFactor = tbInfo.numer / (1e9*tbInfo.denom) ; // ns->s
  }

  return mach_absolute_time() * conversionFactor ; // seconds
}

double machGetClockDiffS()
{
  static double lastTime = 0;

  double currentTime = machGetClockS() ;

  double diff = currentTime - lastTime ;

  lastTime = currentTime ; // update for next call

  return diff ; // that's your answer
}

NSTimeInterval 版本

double getClockS()
{
  return [NSDate timeIntervalSinceReferenceDate] ; // NSTimeInterval is always specified in seconds 
}

double getClockDiffS()
{
  static double lastTime = 0 ;

  double currentTime = getClockS() ;

  double diff = currentTime - lastTime ;

  lastTime = currentTime ; // update for next call

  return diff ; // that's your answer
}

结果:

请注意,这两个版本的分辨率都非常好。

IOS SIMULATOR, running frame rate counts (in milliseconds (*1000.0))

MACH_ABS_TIME / NSTimeIntervals
58.557001 / 58.552980
40.558007 / 40.562987
52.207822 / 52.200019
33.742197 / 33.742011
38.498912 / 38.504004
48.872679 / 48.868001
45.012602 / 45.011997
57.858432 / 57.865977
25.044615 / 25.038004


IPAD HARDWARE SAMPLINGS:
33.415041 / 33.416033
33.240167 / 33.239007
33.357542 / 33.357978
33.302833 / 33.302009
33.506750 / 33.509016
33.582250 / 33.582985
33.233958 / 33.232987
33.239042 / 33.237994

*如果您查看这篇文章的编辑历史记录,您可以看到使用 float 代替 double 的危险!

Here's my answer for clock timers using both mach_absolute_time(), based on compute method shown here, and NSDate. The are actually the same in terms of accuracy.

Mach version

double machGetClockS()
{
  static bool init = 0 ;
  static mach_timebase_info_data_t tbInfo ;
  static double conversionFactor ;
  if(!init)
  {
    init = 1 ;
    // get the time base
    mach_timebase_info( &tbInfo ) ;
    conversionFactor = tbInfo.numer / (1e9*tbInfo.denom) ; // ns->s
  }

  return mach_absolute_time() * conversionFactor ; // seconds
}

double machGetClockDiffS()
{
  static double lastTime = 0;

  double currentTime = machGetClockS() ;

  double diff = currentTime - lastTime ;

  lastTime = currentTime ; // update for next call

  return diff ; // that's your answer
}

NSTimeInterval version

double getClockS()
{
  return [NSDate timeIntervalSinceReferenceDate] ; // NSTimeInterval is always specified in seconds 
}

double getClockDiffS()
{
  static double lastTime = 0 ;

  double currentTime = getClockS() ;

  double diff = currentTime - lastTime ;

  lastTime = currentTime ; // update for next call

  return diff ; // that's your answer
}

Results:

Note the resolution on both of these is really good.

IOS SIMULATOR, running frame rate counts (in milliseconds (*1000.0))

MACH_ABS_TIME / NSTimeIntervals
58.557001 / 58.552980
40.558007 / 40.562987
52.207822 / 52.200019
33.742197 / 33.742011
38.498912 / 38.504004
48.872679 / 48.868001
45.012602 / 45.011997
57.858432 / 57.865977
25.044615 / 25.038004


IPAD HARDWARE SAMPLINGS:
33.415041 / 33.416033
33.240167 / 33.239007
33.357542 / 33.357978
33.302833 / 33.302009
33.506750 / 33.509016
33.582250 / 33.582985
33.233958 / 33.232987
33.239042 / 33.237994

*If you look at the edit history of this post, you can see the danger of using float in the place of a double!

叫思念不要吵 2024-09-22 14:59:14

使用NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate]获取开始时间,然后NSLog(@"Operation take %f秒。", [NSDate timeIntervalSinceReferenceDate] - startTime);在最后。

Use NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate] to get a start time, and then NSLog (@"Operation took %f seconds.", [NSDate timeIntervalSinceReferenceDate] - startTime); at the end.

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