How would I make a timer that counts frames?

Hi there,

Right now, I am making a debugging module, and I want to implement a module.time() function for benchmarking. I want it to count how many frames it takes to run the code after it, and then have a module.timeEnd() function that ends the frame counting. How would I do it? I am not entirely sure as I haven’t really done any sort of benchmarking before.

Personally, I would use tick but I do not know what you’re exactly looking for.

This will be ran in a local script right?

local RunService = game:GetService('RunService')

local module = {  }

module.timing = false
module.frames = 0

function module:timeStart()
    module.timing = true
    module.frames = 0

    local Connection

    Connection = RunService.RenderStepped:Connect(function()
        if not module.timing then Connection:Disconnect() end

        module.frames += 1

function module:timeEnd()
    module.timing = false

    return module.frames

return module

Doesn’t heartbeat and related functions in the RunService Run every frame?

Yes, but this is what OP is asking for. I’m not sure what the best event is, but I would assume RunService.Stepped is the best for benchmarking, since it literally tells you the amount of frames that have passed and is in between RenderStepped and Heartbeat.


.Stepped fires before the physics simulation every frame
.RenderStepped fires before the screen is rendered every frame
.Heartbeat fires after the physics simulation every frame

So in reality, it comes down to if you want physics or render, and before or after.

1 Like

One simple solution would be to use love.timer.getTime to measure the duration of the code.
local elapsed = 0
local start = love.timer.getTime()
– run your code here
elapsed = love.timer.getTime() - start

Returns the time since the start of the game. […] This function returns the time in seconds.

love.timer.getTime can also be used in conjunction with love.timer.step to measure the time that elapsed during this frame. local step

function love.update(dt)
– run your code here
step = dt

function love.draw()"Time of last frame: "…step)

Sets the “update” delta. […] The arguments are optional, but should always be given in the order they appear in this description.

dt parameter in love.update is the time that elapsed since the last frame.

Oh sorry, should probably mentioned I solves this issue!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.