I need to know what's the less time / memory consuming way to store the infos about each tile in a 2D table. The first thing that came to my mind was creating a table holding the infos, and then storing in each tile the string or the number used to access these infos, like:

tilesTable = {
    ["sand"] = {
        walkable = true,
        image = "sand.png",
    ["sea"] = {
        walkable = false,
        image = "sea.png" --...and so on

map = {{"sea", "sand", "sand"},
       {"sea", "sea", "sand"},
       {"sea", "sea", "sea"}}

And then accessing their informations like


And the same would be with number indexes.

But then I tought that when trying to access the infos for each tile, I am first accessing the _G table, then the map table, then the map[x] table, then using the result to access again the _G table and the tilesTable, so I tought to store in each position the reference to a global / local table, like

--[[local]] sand = {walkable = true, image = "sand.png"}
--[[local]] sea = {walkable = false, image = "sea.png"}

map = {{sea, sand, sand},
       {sea, sea, sand},
       {sea, sea, sea}}

So that then I just have to call


Which i suspect being faster, especially if the table is stored as a local variable.

What should I do? Again, what is the way that is faster and consumes less memory?

  • 3
    \$\begingroup\$ What you should always do when you suspect that one way of doing things is faster and consumes less memory but you are not sure about it: Measure it. \$\endgroup\$ – Philipp May 16 '16 at 21:01
  • \$\begingroup\$ Agree w/ @Philipp about measuring. Also, a bit more info would help - storing data on 100 tiles is a very different thing than doing the same for millions of tiles. Finally, keep in mind increased speed often comes at the cost of increased memory use. \$\endgroup\$ – Pikalek May 17 '16 at 2:43
  • \$\begingroup\$ The player will be able to explore as far as it's memory lets it explore. I don't have a dynamic load and unload chunks system yet but even if I had it avoiding wasting memory is always great. \$\endgroup\$ – user6245072 May 17 '16 at 4:38
  • \$\begingroup\$ Also I know how to test how fast something is - but testing how much memory it takes it's a mistery for me. \$\endgroup\$ – user6245072 May 17 '16 at 4:39
  • \$\begingroup\$ @user6245072 The quick and dirty way to measure memory consumption is to just check the task manager and look for how much memory the application consumes. The more accurate way would be material for a new question on stackoverflow.com. \$\endgroup\$ – Philipp May 17 '16 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.