Skip to content

Instantly share code, notes, and snippets.

@PhilipDukhov
Created May 5, 2023 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PhilipDukhov/c4e6bb78376b25b273b3b794ffd8a8c5 to your computer and use it in GitHub Desktop.
Save PhilipDukhov/c4e6bb78376b25b273b3b794ffd8a8c5 to your computer and use it in GitHub Desktop.
@Composable
fun FpsCounter(
modifier: Modifier = Modifier,
) {
var fps by remember {
mutableStateOf("")
}
LaunchedEffect(Unit) {
val frames = mutableListOf<Long>()
while (true) {
val frame = awaitFrame()
frames.add(frame)
frames.removeIf { frame - it > 1_000_000_000 }
if (frames.count() > 1) {
fps = (frames.count() / ((frames.last() - frames.first()).toFloat() / 1_000_000_000f)).roundToInt()
.toString()
}
}
}
Text(
fps,
color = Color.Black,
modifier = modifier
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment