How to get FPS

Edit: Don’t use the code below! Use https://github.com/britzl/defold-metrics

The below code below is faulty because of the way the engine updates dt in some cases, you need to sample real world like like the Metrics version above does.


Simple easy to use module !

local M = {}

local frames = {}
local frame_sum = 0
local frame_index = 1
local MAX_SAMPLES = 10
M.average_ms = 0
M.raw_average_time = 0
M.fps = 0

function M.update(dt)
	if frames[frame_index] then frame_sum = frame_sum - frames[frame_index] end
	frame_sum = frame_sum + dt
	frames[frame_index] = dt
	frame_index = math.fmod(frame_index + 1, MAX_SAMPLES)
	M.raw_average_time = frame_sum / MAX_SAMPLES
	M.average_ms = math.floor(M.raw_average_time * 100000) / 100
	M.fps = math.floor(1.0 / M.raw_average_time * 100) / 100
end

return M

fps.lua (535 Bytes)

local fps = require("utils.fps")
function update(self, dt)
	fps.update(dt)
	print(fps.average_ms .. " ms" , fps.fps .. " FPS", fps.raw_average_time)
end

Should you display FPS when users want to see it? Yes! It’s important to follow user expectations. But you should also show the ms too. Modern games are beginning to show both instead of only FPS which is good. For example, here is Fortnite

2018-08-22%2000_28_36-Window

And as always required reading on this subject
https://www.mvps.org/directx/articles/fps_versus_frame_time.htm

10 Likes

Good one! Thanks for sharing. I created a similar thing a while back: https://github.com/britzl/defold-metrics

3 Likes

Thanks to that, I can see that in an Android release a project works with ~40 FPS and ~20ms, while a quick build in Defold goes with ~120 FPS and ~4ms. Do you know the possible reasons?

I need to know more about the project, but the fact that a desktop build usually can do more in less time due to more processing power isn’t surprising. What could be very taxing on a mobile could be next to nothing on a desktop machine.

4 Likes