Created
April 17, 2025 08:55
-
-
Save jacobsapps/4c9d445cfc9910cc328a7efe011b6580 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@State private var fps: Double = 0 | |
@State private var lastTimestamp: TimeInterval = 0 | |
private let smoothingFactor: Double = 0.1 | |
// ... | |
.task { | |
for await link in CADisplayLink.events() { | |
updateFPS(link) | |
} | |
} | |
// ... | |
private func updateFPS(_ link: CADisplayLink) { | |
let currentTime = link.timestamp | |
let elapsedTime = currentTime - lastTimestamp | |
lastTimestamp = currentTime | |
if elapsedTime > 0 { | |
let newFPS = 1.0 / elapsedTime | |
fps = (fps * (1 - smoothingFactor)) + (newFPS * smoothingFactor) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment