Little Advice

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]

In addition to this, I’d suspect that it’s faster to do that at any point - not just in a loop. When it’s in a loop, you can see the difference most, though.

C [import]uid: 147322 topic_id: 35632 reply_id: 141869[/import]

Good for people that want to create roguelikes! The first step on an incredibly rewarding, potentially frustrating path! [import]uid: 135394 topic_id: 35632 reply_id: 142108[/import]

Quick question: why are you using grid[w…"."…h] instead of grid[w][h]? I’m always on the prowl for new, ever faster ways of accessing tables. [import]uid: 99903 topic_id: 35632 reply_id: 142130[/import]

That’s just my preference - you can use either way. I use that because I like to keep my tables holding their own values and not get cluttered up - if you have a bunch of “w” tables with a bunch of “h” tables inside of them, personally, I don’t feel as comfortable.

Both ways are fine, though :slight_smile:

C [import]uid: 147322 topic_id: 35632 reply_id: 142156[/import]

I definitely understand the role of preferences and… I guess programming “style” would be a good thing to call it. I often see code that just looks strange to me because it isn’t the way I learned, to the point of it being hard for me to read.

Anyway, thanks for posting this insight! It’s something I never gave much thought, but it definitely makes a huge difference. [import]uid: 99903 topic_id: 35632 reply_id: 142166[/import]

You guys should take a look at this: http://www.lua.org/gems/sample.pdf (if you haven’t already:))

It goes over some performance tips for Lua and it also goes over what Caleb has mentioned here! Well worth a look if you have the time. [import]uid: 69826 topic_id: 35632 reply_id: 142177[/import]

Nice link! :slight_smile:

C [import]uid: 147322 topic_id: 35632 reply_id: 142181[/import]

In addition to this, I’d suspect that it’s faster to do that at any point - not just in a loop. When it’s in a loop, you can see the difference most, though.

C [import]uid: 147322 topic_id: 35632 reply_id: 141869[/import]

Good for people that want to create roguelikes! The first step on an incredibly rewarding, potentially frustrating path! [import]uid: 135394 topic_id: 35632 reply_id: 142108[/import]

Quick question: why are you using grid[w…"."…h] instead of grid[w][h]? I’m always on the prowl for new, ever faster ways of accessing tables. [import]uid: 99903 topic_id: 35632 reply_id: 142130[/import]

That’s just my preference - you can use either way. I use that because I like to keep my tables holding their own values and not get cluttered up - if you have a bunch of “w” tables with a bunch of “h” tables inside of them, personally, I don’t feel as comfortable.

Both ways are fine, though :slight_smile:

C [import]uid: 147322 topic_id: 35632 reply_id: 142156[/import]

I definitely understand the role of preferences and… I guess programming “style” would be a good thing to call it. I often see code that just looks strange to me because it isn’t the way I learned, to the point of it being hard for me to read.

Anyway, thanks for posting this insight! It’s something I never gave much thought, but it definitely makes a huge difference. [import]uid: 99903 topic_id: 35632 reply_id: 142166[/import]

You guys should take a look at this: http://www.lua.org/gems/sample.pdf (if you haven’t already:))

It goes over some performance tips for Lua and it also goes over what Caleb has mentioned here! Well worth a look if you have the time. [import]uid: 69826 topic_id: 35632 reply_id: 142177[/import]

Nice link! :slight_smile:

C [import]uid: 147322 topic_id: 35632 reply_id: 142181[/import]