#Heatbeat vs While loop?
1 messages · Page 1 of 1 (latest)
It depends on what you are doing. Neither are expensive.
To benchmark anything, you can use a timestamp function like tick or time and subtract one time stamp from another
local startTime = tick()
a = 1; a+=1;
warn("that took " .. tostring(tick() - startTime) .. " seconds")
Heartbeat and task.wait both are almost identical but the best option is to use task.wait without the while true do
alright thanks
like with recursion?
local timeTillNextCallBack = 60 -- seconds
task.delay(timeTillNextCallBack, callback)
For ur case specifically while loop, otherwise ur are technically wasting cpu with heartbeat
Generally speaking, heartbeat over task.wait loop is better
-- takes around 28ms on Suphi's computer to run 20000 Heartbeat connections
local runService = game:GetService("RunService")
local function Loop()
end
for i = 1, 20000 do
runService.Heartbeat:Connect(Loop)
end
-- takes around 28ms on Suphi's computer to run 15000 while true do loops
local function Loop()
while true do
task.wait()
end
end
for i = 1, 15000 do
task.defer(Loop)
end
15k deferments
🤠
I look at CPU time per frame
Every frame with 20000 heartbeat connections takes 28ms of CPU time
And with 15000 while true do loops takes 28ms of CPU time
I use the micro profiler maybe this video will help
https://youtu.be/BbIPalpAfaI
Discord: https://discord.gg/bEn49K5JUt
Patreon: https://www.patreon.com/Suphi
Donate: https://www.roblox.com/games/7532473490
https://create.roblox.com/docs/optimization/microprofiler
https://create.roblox.com/docs/scripting/scripts/parallel-scripting
https://create.roblox.com/docs/reference/engine/classes/Actor
https://create.roblox.com/docs/r...
accurate enough to tell what's faster or slower
if the difference was only around 1000 then I would say there the same but with a delta of 5000 its obvious
inherently flawed cause you're defering it to the next engine cycle
course it's going to be slower lol
It's slower because you're running it slower

i measuring all cycles not the current cycle
defer only runs when the script starts
i should make an album of all your suphi moments
if you want accurate then use heartbeat
if u want yield the script then use while true do
to access deltatime you can just do (not accurate in my opinion)
while true do
local deltaTime = task.wait()
end
