K
K
kunjut192020-06-13 19:21:02
Game development
kunjut19, 2020-06-13 19:21:02

An array of objects or an array of "arrays"?

If we imagine that I am writing a game with a tile map (something like Civilization or Heroes), what would be the best way to store information about tiles in terms of performance?
There are a lot of cells (let's say 100,000), and each cell stores a lot of information - which player the cell belongs to, the type of terrain, resources, whether the unit is occupied, information about neighboring cells (if there is a sea cell nearby, then you can build a port). The player captures the castle, and now you need to run through a large number of cells around the castle and change their belonging to one or another player.
This is just a model to make it easier to understand exactly what I mean. From the point of view of such a model, what is faster to work with - with a two-dimensional array of objects or with a three-dimensional array of strings and numbers? If we neglect the fact that it will be more convenient to understand the record than , then what would be the best choice? I do not know if there will be a difference depending on the language, so I do not focus the question on a specific language. map[463][537].type='grass'map[463][537][31]='grass'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergei Chamkin, 2020-06-13
@Sergei1337

Made now the simplest measurement. The difference is not great + -15%.
Wins a two-dimensional array of objects.
(measured on java)

T
Timur Pokrovsky, 2020-06-13
@Makaroshka007

c# code:

using System;
using System.Diagnostics;

namespace ConsoleApp19 {
  class Program {
    struct Cell {
      public string type;
      public string player;
      public bool isUnit;
    }

    public static void Main() {
      const int size = 1_000;

      object[,,] objs = new object[size, size, 3];
      Cell[,] cells = new Cell[size, size];

      Stopwatch timer = new Stopwatch();
      timer.Start();
      for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
          objs[i, j, 0] = "type";
          objs[i, j, 1] = "player";
          objs[i, j, 2] = false;

          string type = (string)objs[i, j, 0];
          string player = (string)objs[i, j, 1];
          bool isUnit = (bool)objs[i, j, 2];
        }
      }
      timer.Stop();
      Console.WriteLine(timer.ElapsedMilliseconds);

      timer.Reset();
      timer.Start();
      for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
          cells[i, j].type = "type";
          cells[i, j].player = "player";
          cells[i, j].isUnit = false;

          string type = cells[i, j].type;
          string player = cells[i, j].player;
          bool isUnit = cells[i, j].isUnit;
        }
      }
      timer.Stop();
      Console.WriteLine(timer.ElapsedMilliseconds);
    }
  }
}

Output:
89 (3 dimensional array)
18 (2 dimensional array)

G
Griboks, 2020-06-13
@Griboks

Someone on habré researched this question. You can search. Brief summary: an array of arrays is faster because i is the simplest pointer arithmetic, when objects require references, memory control, alignment, and the like.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question