0

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't seem that there is a definite answer, so I'll try structs and then the multidimensional array.
1

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).

1 Comment

Unless most of your algorithms access only one field and a single field fits in the cache and the entire array doesn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.