以编程方式计算iOS应用程序的FPS(每秒帧数)的方法有多less种?

由于我们正在以programmatically进行讨论,因此乐器不在考虑之列。

提前列出一些参考资料:

  • 计算iphone应用程序的fps(每秒帧数)
  • iOS屏幕上显示FPS(无仪器)
  • iOS界面以什么帧率运行animation?

1.使用CADisplayLink

根据文件,

持续时间属性提供了帧之间的时间量。 您可以在您的应用程序中使用此值来计算显示的帧速率…

所以在我的演示项目中,我将一个displayLink添加到mainRunLoop for UITrackingRunLoopMode:

 self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(screenDidUpdated)]; [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode]; 

并使用duration来计算FPS。 但是出于我的预期, duration总是0.016667(〜FPS 60),不pipetableView是否顺利滚动。

我如何使tableView滞后是添加一行- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

 NSData *data = [NSData dataWithContentsOfURL:url]; 

然后我转向displayLink.timestamp ,它工作。

2.观察drawRect:

我的第二个想法是观察drawRect:我认为当tableView滚动时,它的drawRect:将被逐帧调用,然后我可以根据drawRect: callings之间的时间差来计算FPS。

但它失败了,导致drawRect:只被调用一次(而单元格多次)。 这种方法类似于Using CADisplayLink ,但也许我select了错误的位置/方法(如drawRect: Using CADisplayLink来观察,我推荐的方法是观察?

问题:

  1. 仪器如何精确测量FPS?
  2. 文件是using duration to calculate FPS错误的吗?
  3. 观察计算FPS的最佳方法是什么?

谢谢!

这工作,但需要一些设置。

  1. 设置一个CADisplayLink,并将frameInterval作为默认值(每帧一次)
  2. 这个CADisplayLink实际上需要添加到runLoop中,并且至less触发一次才能到达#3
  3. 一旦callback,或在callback中,您可以获得帧速率。 下面是一些Swift代码给你的想法,但是其中displayLink是你的CADisplayLink实例

     var framesPerSecond : Int { var retVal : Int = 60 // default value, just in case link.duration is still 0 if let link = displayLink where link.duration > 0 { retVal = Int(round(1000 / link.duration)/1000) } return retVal; } 

作为CADisplayLink状态的文档 , duration基本上只是1 / maximumFramesPerSecond 。 您需要检查targetTimestamptimestamp之间的差异以检测丢弃的帧:

duration属性提供了在maximumFramesPerSecond帧之间的时间量。 要计算实际的帧持续时间,请使用targetTimestamp – timestamp。