Eugene
Eugene

Reputation: 11545

Performance comparison Integer Multidimensional array vs Array of Structures

Are multidimensional arrays better than an array of structures if there's only one datatype? For example would an array of:

struct data{
    short datapointa;
    short datapointb;
    short datapointc;
}

be better than

short data[100][100][100][100];

What if there are several datatypes, would several arrays be better than an array of struct?

Upvotes: 0

Views: 764

Answers (2)

Paul R
Paul R

Reputation: 213190

It will depend mainly on your access pattern. You should choose whichever data structure gives you best locality of access - ideally you should be accessing memory contiguously, in order to make best use of cache and minimise memory bus traffic. Obviously this will depend on what kind of algorithm(s) you are using with these data structures and to some extent how you implement these algorithm(s).

Upvotes: 1

Martin Beckett
Martin Beckett

Reputation: 96167

1, Measure it

2, You need to look at what the compiler actually generates as code, then consider things like the size of cache and number of cores.

3, Then measure it

There is very little point thinking about micro-optimizations until you have measured if the speed is a problem and if there is any difference in speed.

Upvotes: 2

Related Questions