Hello, I’m wondering how I can run loops like these…
local OneMillion = 1000000
local clock = os.clock()
for i = 1, OneMillion do
-- test()
end
print("Time", os.clock() - clock)
without this
-- 13:05:35.119 Script timeout: exhausted allowed execution time - Server - Script:7
Interrupting that specific loop.
Edit:
local function test()
-- etc..
end
-- vs
local test = function()
-- etc..
end
-- Preferences/Readibility vs Optimization
They should finish at similar times.
SOTR654
(SOTR654)
March 17, 2021, 7:14pm
#2
Add wait()
local OneMillion = 1000000
local clock = os.clock()
for i = 1, OneMillion do
wait()
-- test()
end
print("Time", os.clock() - clock)
using wait(0) also works well.
Im fairly certain it gives a delay of about 25 miliseconds.
2 Likes
If you want to increase the number as well, you need to add the increment to the for loop
for example:
local OneMillion = 1000000
local clock = os.clock()
for i = 1, OneMillion, 1 do -- Third value is the increment, in this case we're adding one
-- test()
wait()
end
print("Time", os.clock() - clock)
I don’t want the loop to have a delay. I’m testing for different speed rates on functions that do similar tasks. Choosing which one is the fastest.
@Afraid4Life
Yes I know that, the default is 1 which is what I’m planning on using.
im pretty sure if you don’t want to have that delay then you’re going to have to add the increment.
SOTR654
(SOTR654)
March 17, 2021, 7:24pm
#8
Hmm… Could use coroutines.
local OneMillion = 1000000
local clock = os.clock()
local Function = coroutine.create(function()
-- Code --
end)
for i = 1, OneMillion do
coroutine.resume(Function)
end
print("Time", os.clock() - clock)
It would be instantaneous.
I could but the time the function took to run 1 million times won’t be recorded.
SOTR654
(SOTR654)
March 17, 2021, 7:28pm
#10
Maybe this:
local OneMillion = 1000000
local clock = os.clock()
for i = 1, OneMillion do
wait(1)
-- test()
end
print("Time", os.clock() - clock - OneMillion)
This should give what you want.
You can’t disable script timeout, but you can add a yield/wait to a loop to stop it.
comsurg
(cog)
July 30, 2021, 7:09am
#13
Timeouts exist so that the script didn’t delay task manager too much.