Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you quickly iterate through a table with tens of thousands of records?

Exactly what the title says... I ran into a snag where a for in pairs isn't fast enough to check through tens of thousands of records (only does 60 per second). I need to check to see if a part's specific position is already inside a table so that when I generate another part from the user's input, the part will be placed in an area that has never been occupied.

I couldn't find any more information on something called maps and would appreciate some clarity as it may seem as though it could solve my problem.

Here is some quick code just to help show what I mean:


local myTable = {}
local part = game.Workspace.Part
local testerPart = game.Workspace.testerPart
local testPosition = Vector3.new(10,10,10)
local counter = 0
---- The following while loop is just to show you guys, I know they are all the same values
while counter < 1,000 do
	part:Clone()
	table.insert(myTable, part.Position)
	counter = counter + 1
end

---- This part is just too slow
for i, v in pairs(myTable) do
	if v == testPosition then
		break -- since it's already inside the table
	elseif i = #myTable then      ---if it reached the last record without finding a match
		testerPart:Clone()       ----then copy the testerPart
		testerPart.Parent = game.Workspace       --- parent it to Workspace
		testerPart.Position = testPosition       -- give it the position
	end
		
end

2 comments

0

line 2 declares part as the part's position, and line 8 is cloning it. you cant clone a position dotdotdot

0

whoops, like I said that was very quick code that I typed up just to show what I mean

1 answer

✓ Accepted answer

You can use something called a binary search. It's a different method of searching through tables than a standard line-by-line check, or linear search. It assumes that you have a table sorted in some manner (like a table of integers from 0-10 to provide a basic example) and then searches by cutting the results in half.

For example:

Let's say we have a table like the following

exampleTable = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Now let's say we want to find the value 7. A linear search (what happens when you do "for i, v in pairs()") would go something like this. 0? no. 1? no. 2? no. 3? no. 4? no. 5? no. 6? no. 7? yes.

A binary search would go something like this:

5? no. greater. 8? no. lesser. 6? no. greater. 7? yes.

In some cases, it's not faster (for example, if the value you were looking for was 1), but in your case it might help a lot.

Some sample Lua code for a binary search can be found here

Sorry I didn't write out the code myself, I don't have the time right now. Hope this helps.

1 comment

0

As soon as I read this I had to test out how many passes it would take for 10,000 records and it cuts it down to 14 passes to find the number 1, 15 pases for 20,000, 16 passes for 40,000 etc... This definitely solved my problem, thank you very much!

This discussion is preserved as it was. New answers, comments, and votes are closed.