#Heatbeat vs While loop?

1 messages · Page 1 of 1 (latest)

dry peak
#

I'm wondering which is more performant, using a heartbeat event and having an elapsed time variable to play a callback at intervals or just using a while loop with task.wait(interval). I'm not really sure how I would bench mark this.

young burrow
#

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")
sand shale
#

Heartbeat and task.wait both are almost identical but the best option is to use task.wait without the while true do

dry peak
#

alright thanks

sand shale
#
local timeTillNextCallBack = 60 -- seconds
task.delay(timeTillNextCallBack, callback)
glacial thistle
#

For ur case specifically while loop, otherwise ur are technically wasting cpu with heartbeat

#

Generally speaking, heartbeat over task.wait loop is better

sand shale
#
-- 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
glacial thistle
#

How do you even benchmark that cmonman

#

You’re deferring it anyways

young burrow
#

15k deferments Logo 🤠

sand shale
glacial thistle
#

Extremely inaccurate test

#

You realize cpu is capped by a lot

sand shale
#

if the difference was only around 1000 then I would say there the same but with a delta of 5000 its obvious

glacial thistle
#

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

sand shale
#

defer only runs when the script starts

glacial thistle
fallen canyon
#

if u want yield the script then use while true do

fallen canyon