Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Since we are talking about programmatically, Instruments are not under my consideration.

Some reference listed in advance:

  • Calculate fps (frames per second) for iphone app
  • Display FPS on iOS onscreen (without Instruments)
  • At what framerate does the iOS UI run animations at?

1. Using CADisplayLink

According to the doc,

The duration property provides the amount of time between frames. You can use this value in your application to calculate the frame rate of the display...

So in my demo project, I add a displayLink to mainRunLoop for UITrackingRunLoopMode:

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

and use duration to calculate FPS. But out of my expectation, the duration is always 0.016667 (~ FPS 60) not matter the tableView scrolls smoothly or not.

How I make the tableView lag is to add one line in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath:

NSData *data = [NSData dataWithContentsOfURL:url];

Then I turned to displayLink.timestamp, and it worked.

2. Observing drawRect:?

My second idea is to observe drawRect:, I thought when the tableView scrolls, its drawRect: would be called frame by frame, then I could calculate the FPS according to the time diff between drawRect: callings.

But it failed, cause the drawRect: was called only once (while cells for many times). This way is similar to Using CADisplayLink, but maybe I chose the wrong position/method(like drawRect:) to observe, any recommended method for me to observe?

Questions:

  1. How the Instruments measure the FPS accurately?
  2. Is the doc said using duration to calculate FPS wrong?
  3. What's the right/best method to observe to calculate the FPS?

Thanks!

like image 306
Jason Lee Avatar asked Nov 12 '14 13:11

Jason Lee


People also ask

What is the formula for fps calculation?

To calculate frames per second, you just take the number of rendered frames and divide it by the seconds passed. Now it is possible for there to be a very small amount of time passed for the first frame and have it give us a really high fps.

What is fps on mobile?

Frame rate (also known as Frames Per Second or (FPS) is one of the best metrics you can capture to quantify the visual fluidity of a game. In most scenarios, the higher the frame rate, the better the experience for the gamer.


2 Answers

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.

like image 121
Tamás Zahola Avatar answered Nov 05 '22 06:11

Tamás Zahola


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;
    }
    
like image 1
Dan Rosenstark Avatar answered Nov 05 '22 04:11

Dan Rosenstark