Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

SwiftでFPSカウンターを作る

ソースコード

FPSCounter.swift
import Foundation

class FPSCounter {
    private var baseTime: Int!
    private var count: Int = 0
    private var fps: Float = 0

    func getFPS() -> Float {
        return fps
    }

    func tickStart() {
        baseTime = timeSince1970Millis()
    }

    func tick() {
        count = count + 1;
        let now = timeSince1970Millis()
        if (now - baseTime >= 1000) {
            fps = Float(count * 1000) / Float(now - baseTime)
            baseTime = now
            count = 0
        }
    }

    private func timeSince1970Millis() -> Int {
        return Int((Date().timeIntervalSince1970 * 1000.0).rounded())
    }
}

使い方

ViewController.swift
final class ViewController: UIViewController {

    private var fpsCounter = FPSCounter()

    override func viewDidLoad() {
        super.viewDidLoad()
        fpsCounter.tickStart()
    }

    private func hoge() {
        // なんらかの処理

        fpsCounter.tick()
        print("fps: \(fpsCounter.getFPS())")
    }
}
k-yamada-github
Kotlin, Swift, Android, iOS, Vue.js, AWS
sonix
Mobility Tech Company「世界」をリードするイノベータ集団でありたい
https://www.sonix.asia/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away