iOS 计算fps

2019-12-29  本文已影响0人  CocoaJason
[[CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction:)] addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
- (void)displayLinkAction:(CADisplayLink *)link {
    static NSTimeInterval lastTime = 0;
    static NSInteger frameCount = 0;
    if (lastTime == 0) {
        lastTime = link.timestamp;
        return;
    }
    frameCount ++;
    NSTimeInterval paseTime = link.timestamp - lastTime;
    if (paseTime > 1) {
        NSInteger fps = frameCount / paseTime;
        lastTime = link.timestamp;
        frameCount = 0;
        NSLog(@"%ld",fps);
    }
}
2019-12-29 20:45:16.035014+0800 CADisplayLink[14509:274268] 52
2019-12-29 20:45:17.034081+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:18.034943+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:19.034156+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:20.034959+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:21.034505+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:22.034900+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:23.034121+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:24.034044+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:25.034870+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:26.033931+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:27.034908+0800 CADisplayLink[14509:274268] 59
2019-12-29 20:45:28.034852+0800 CADisplayLink[14509:274268] 59
上一篇下一篇

猜你喜欢

热点阅读