I was footling around and I found out an important thing that might help other people.
When creating a grid of tables, it is faster to add values inside of the table instead of outside of it.
For example, I’ll make a grid of tables:
[lua]local grid={}
for w=1, 500 do
for h=1, 500 do
grid[w…"."…h]={}
end
end[/lua]
Ok, now we have a blank 500x500 grid of tables. But it’s still not a grid until we add X and Y values to them. So now I’ll add X and Y values. For that, I’ll need to create a “size” variable that will be the width and height of the grid’s “cells”.
[lua]local grid={}
local size=20
for w=1, 500 do
for h=1, 500 do
grid[w…"."…h]={}
grid[w…"."…h].x, grid[w…"."…h].y=w*size, h*size
end
end[/lua]
Now here’s where the problem comes in. Do I do it that way, or do I do it like this:
[lua]local grid={}
local size=20
for w=1, 500 do
for h=1, 500 do
grid[w…"."…h]={
x=w*size
y=h*size
}
end
end[/lua]
Actually, it turns out that the second method is just about exactly twice as fast as the first method. So when you want to add parameters, the conclusion I came to was to add them when you create the table.
You can check it and see with this block of code:
[lua]local grid={}
local size=5
local useFastMethod=false
local startTime=system.getTimer()
if useFastMethod then
for w=1, 500 do
for h=1, 500 do
grid[w…"."…h]={
x=w*size,
y=h*size
}
end
end
else
for w=1, 500 do
for h=1, 500 do
grid[w…"."…h]={}
grid[w…"."…h].x, grid[w…"."…h].y=w*size, h*size
end
end
end
print(system.getTimer()-startTime)[/lua]
Hope this helps someone!
Caleb
[import]uid: 147322 topic_id: 35632 reply_id: 335632[/import]
