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() {
//Certains traitements
fpsCounter.tick()
print("fps: \(fpsCounter.getFPS())")
}
}
Recommended Posts