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.
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
end)
end
function module:timeEnd()
module.timing = false
return module.frames
end
return module
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.
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
end
function love.draw()
love.graphics.print("Time of last frame: "…step)
end
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.