It depends on your usage. Because either way, the addition of multiple indexes would be very slow, and because of running very fast, can take more processing power, and may overwhelm the game, causing it to lag, or potentially crash.
If you are adding a single number, table.create()
would the fastest for this usage, otherwise a for
loop would be your best bet.
coroutines
will actually make your loop slower here, rather than give you any speed boost, as evident by this benchmark test i did testing both with, and without any multithreading, The results are pretty big when ran:
-- two separate tables to better compare both results
local v1 = {}
local v2 = {}
local s1 = os.clock() -- time start
for i = 1, 1e6 do -- run this code a million times
table.insert(v1, i)
end
print(`V1: {os.clock() - s1}`) -- print test results
-- will give us the time difference these took
-- which basically determine which is more performant than the other
task.wait(1) -- time to wait before next test
-- repeat!
local s2 = os.clock()
for i = 1, 1e6 do
coroutine.wrap(function()
table.insert(v2, i)
end)()
end
print(`V2: {os.clock() - s2}`)
from my tests, V1
(the loop without multithreading) took about 34 milliseconds on average to complete, while V2
(the test with multithreading), took 1.24 seconds, so its safe to say what you did made it slower.
If we are comparing this to the actual game, 60 frames is (which when 1/60) roughly 16 - 17 milliseconds, V1
would be the most performant as it takes the least amount of time to fully run, while V2
if run constantly, will lag our game, the same can be said for V1
foreach
is deprecated, as has been for a long time.