Best way to put 1 million numbers in a table at lightspeed?

Hello Roblioxians, quick question, what is the best way to add over a million numbers in a table at lightspeed. I tried for I looping but it gave me a script timeout error. Would really appreciate the help!

My Code :

for i = 10000000000,0,-1 do
      coroutine.wrap(function()
            table.insert(tab, i)
      end)()
end
7 Likes

dont forget to add a wait
they may be why you got the script timeout
do
wait()
thats the fastest wait i believe

2 Likes

But that will make it take time, I said I wanted it to go at lightspeed.

2 Likes

What about table.create(10000000000, 1)

5 Likes

i don’t think it can go faster than

for i = 10000000000,0,-1 do
      coroutine.wrap(function()
            table.insert(tab, i)
      end)()
      task.wait()
end

unless you use something other than table.insert because your computer can’t proccess all of that instantly

1 Like

yeah, your very correct
but i do believe that is the fastest way
(i remembered that task.wait() is faster lol)

1 Like

Well roblox runs on servers which have purposely limited amounts of RAM and computing power, and you’re trying to add 10 BILLION numbers, over 10,000 times more than 1 million. Taking 80 GB OF RAM! That is more than many workstation computers, and that would cost a lot.

2 Likes

I think so, I don’t know

local numbers = {} --Here you must fill the table with the numbers you want to add

local sum = 0
table.foreach(numbers, function(_, value)
    sum = sum + value
end)

print(sum) -- The sum result will be printed on the output console


1 Like
local n = 2^20
for i = 1, 1000000001 do
    tab[i] = 1000000001 - i
    if i % n == 0 then
        task.wait()
    end
end

This can take about 40 seconds for 1 billion iterations.
For 10 billion iterations, the server can run out of RAM.

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.

1 Like

Yeah for just a million values there is no reason to make this have any strange things other than a basic for loop. On my machine the first one took 16 ms and the second one took 720 ms, just a ton of overhead.

I tried it agian using table.create, then indexing the array and setting it that way and now it’s down to 8 ms on my machine.


local testt = table.create(1e6,0)

for i = 1, 1e6 do
	testt[i] = i
end

That is 10 billion and not 1 million, lol.
What on earth do you need such thing for tho!? You will sacrifice one of performance or speed for the other.
Anyways, did some testing and got these results.


Method 1: Spawning a new thread and inserting

local function fillTable()
	local t = {}
	local start = DateTime.now().UnixTimestampMillis
	
	for i = 1, 1_000_000 do	
		task.spawn(table.insert, t, i)
		
		if i % 100000 == 0 then task.wait() end
	end
	
	print(`took {DateTime.now().UnixTimestampMillis - start}ms to put one million items into the table.`)
end

print("starting")

for i = 1, 20 do
	fillTable()
	
	task.wait(.5)
end

print("done")


Method 2: Inserting without creating a new thread

local function fillTable()
	local t = {}
	local start = DateTime.now().UnixTimestampMillis
	
	for i = 1, 1_000_000 do	
		table.insert(t, i)
		
		if i % 100000 == 0 then task.wait() end
	end
	
	print(`took {DateTime.now().UnixTimestampMillis - start}ms to put one million items into the table.`)
end

print("starting")

for i = 1, 20 do
	fillTable()
	
	task.wait(.5)
end

print("done")


Method 3: Creating a table beforehand, then editing the values

local function fillTable()
	local t = table.create(1_000_000, 1)
	local start = DateTime.now().UnixTimestampMillis
	
	for i = 1, #t do	
		t[i] = i
		
		if i % 100000 == 0 then task.wait() end
	end
	
	print(`took {DateTime.now().UnixTimestampMillis - start}ms to put one million items into the table.`)
end

task.wait(5)

print("starting")

for i = 1, 20 do
	fillTable()
	
	task.wait(.5)
end

print("done")


The results are pretty clear, the second method is way faster and more consistent. Just use this code then:

local t = {}

for i = 1, 1_000_000 do	
	table.insert(t, i)

	if i % 100000 == 0 then task.wait() end
end

Note that I have background tasks running like Lively, Rainmeter, Spotify, etc. Running without them would produce faster tests but won’t affect the overall result.

1 Like

I don’t think “wait” is needed here because 1 million elements isn’t much so it will just slow down the loop.
(I haven’t tested it)

Yes, but the script will send a timeout every now and then and will do so frequently on lower end devices. Other than that, the OP did 10 billion -in the code he provided- but the title said one million so I don’t know which one he wants. Better safe than sorry.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.