What is the performance impact of empty indexes in arrays?

I’m trying to make it work like an address by multiplying it by hundreds of integers depending on the class type, but this make hundreds of empty indices. There are actually only a few allocations, but will there be any performance degradation in For Each Loop etc. due to empty indexes?

It is hard say how much slower they will be without a context because, as always “it depends on the case” but sparse arrays are definitely to be avoided.

Processing information in a continuous manner avoids cache misses which is a main point of optimization for high performance software like games.

Now I’ll not pretend to be an expert on the matter so you are probably better off researching it on your own but the jest is this:
When you read memory you take a big chunk of it in your processor cache (even bigger in the higher level cache and so on). If the next information you process is in that already fetched chunk it is lightning-fast but if it is not, the whole chunk has to be “flushed” before taking the next chunk and the higher level cache flushed the slower it is (all lower levels are flushed too).

So in your case the performance impact depends on how big the gaps are and how often do you trigger those cache misses. How often you try to read, how often you jump etc. A few missing indecies should not be a problem.

There is a map type that associates keys (which can be int) with data and they are still stored continuously.

P.S.
Here we are not talking at all about the amount of memory wasted.

1 Like

For example,Is there a difference in load between an array with just 5 indexes and an array with 100 empty indexes between them? How to measure cache miss frequency?

I don’t plan on using this array that often. I wish there was a better way to sort class types, but for now this is the only way I can think of.

Why don’t you use a Map container for this? :thinking: