Skip to content

Instantly share code, notes, and snippets.

@elkraneo
Created November 15, 2023 13:49
Show Gist options
  • Save elkraneo/7732c74b0fca1fdcbeab646ab238e546 to your computer and use it in GitHub Desktop.
Save elkraneo/7732c74b0fca1fdcbeab646ab238e546 to your computer and use it in GitHub Desktop.
let sceneUpdateSubscription = content.subscribe(to: SceneEvents.Update.self) { event in
let deltaTime = event.deltaTime
// Accumulate delta time
self.accumulatedTime += deltaTime
self.frameCount += 1
// Calculate FPS every n frames
if self.frameCount >= 10 { // Calculate FPS every 10 frames (1 second at 10 FPS)
if self.accumulatedTime > 0 { // Ensure accumulated time is not zero
let fps = Double(self.frameCount) / self.accumulatedTime
print("FPS: \(fps)")
Task {
await viewStore.send(.didUpdate(fps)).finish()
}
} else {
print("FPS: Infinity") // Handle the case when accumulated time is zero
}
self.frameCount = 0
self.accumulatedTime = 0
}
}
subscriptions.append(sceneUpdateSubscription)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment