How many ways to calculate the FPS (Frames per second) of an iOS App programmatically?

大城市里の小女人 提交于 2019-11-30 21:42:39

This works but requires some setup.

  1. Set up a CADisplayLink and leave the frameInterval as the default (shoots once per frame)
  2. This CADisplayLink needs to actually be added to a runLoop and fire at least once to get to #3
  3. Once that calls back, or inside the callback, you can get the frame rate. Here's some Swift code to give you the idea, but where displayLink is your CADisplayLink instance

    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;
    }
    

As the documentation for CADisplayLink states, duration is basically just 1 / maximumFramesPerSecond. You need to check the discrepancy between targetTimestamp and timestamp to detect dropped frames:

The duration property provides the amount of time between frames at the maximumFramesPerSecond. To calculate the actual frame duration, use targetTimestamp - timestamp.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!