1

I have created a few headers each which holds an array and they are all nested:

PixelArray[17] inside ScreenArray[10] inside MonitorArray[Dynamic Pointer]

This is because I wanted to test it as I went along (Not a competent programmer by a long shot) it was only after this I realised I could have merged PixelArray[17] and ScreenArray[10] into a single 2D array:

PixelArray[10][17] inside MonitorArray[Dynamic Pointer]

My question is whether it would be more efficient to use a single 2D array or not?

1
  • Efficiency is literally the last thing you should be worried about. In your case you probably won't see any difference in performance, but a definite answer can be only obtained by measuring. Commented Oct 24, 2014 at 8:53

3 Answers 3

1

Managing 2D array requires more competance than managing 1D array, but 2D array is more elegant as you have packaging in better form, since you have the data grouping kept together

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

2 Comments

I suppose elegance is something to aim for, but I was wondering on compile whether there is any discernible difference between the methods?
@BuiltonSin They are same from compiler point of view, same amount of data storage, little efficient in 2D as you have less variables to access
1

A single 2D array or an array containing arrays are the very same in C++. Indeed

typedef int InnerArray[20];
InnerArray OuterArray[30];

and

int MyArray2d[20][30];

are basically two ways to say the same thing to a C++ compiler.

Comments

1

The question of how good a particular data layout is in terms of efficiency can only be answered if you take into account how you access that data.

In general you can say that you should group data in memory such that you access memory consecutively or at least locally most of the time, you will then benefit from hardware support such as data streaming or cache efficiency. This is often referred to as "data-oriented design".

That being said, there is probably not much difference to expect from your two suggestions as the memory layout of both is effectively the same.

Comments

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.