lyst lyst - 7 days ago 6
C++ Question

For loop or no loop? (dataset is small and not subject to change)

Let's say I have a situation where I have a matrix of a small, known size where the size is unlikely to change over the life of the software. If I need to examine each matrix element, would it be more efficient to use a loop or to manually index into each matrix location?

For example, let's say I have a system made up of 3 windows, 2 panes per window. I need to keep track of state for each window pane. In my system, there will only ever be 3 windows, 2 panes per window.

static const int NUMBER_OF_WINDOWS = 3;
static const int NUMBER_OF_PANES = 2;

static const int WINDOW_LEFT = 0;
static const int WINDOW_MIDDLE = 1;
static const int WINDOW_RIGHT = 2;

static const int PANE_TOP = 0;
static const int PANE_BOTTOM = 1;

paneState windowPanes[NUMBER_OF_WINDOWS][NUMBER_OF_PANES];


Which of these accessing methods would be more efficient?

loop version:

for (int ii=0; ii<NUMBER_OF_WINDOWS; ii++)
{
for (int jj=0; jj<NUMBER_OF_PANES; jj++)
{
doSomething(windowPanes[ii][jj];
}
}


vs.

manual access version:

doSomething(windowPanes[WINDOW_LEFT][PANE_TOP]);
doSomething(windowPanes[WINDOW_MIDDLE][PANE_TOP]);
doSomething(windowPanes[WINDOW_RIGHT][PANE_TOP]);

doSomething(windowPanes[WINDOW_LEFT][PANE_BOTTOM]);
doSomething(windowPanes[WINDOW_MIDDLE][PANE_BOTTOM]);
doSomething(windowPanes[WINDOW_RIGHT][PANE_BOTTOM]);


Will the loop code generate branch instructions, and will those be more costly than the instructions that would be generated on the manual access?

Answer

First of all: How complex is your function doSomething? If it is (most likely this is so), then you will not notice any difference.

In general, calling your function sequentially will be slightly more effective than the loop. But once again, the gain will be so tiny that it is not worth discussing it.

Bear in mind that optimizing compilers do loop unrolling. This is essentially generating code that will rotate your loop smaller number of times while doing more work in each rotation (they will call your function 2-4 times in sequence). When the number of rotations is small and fixed compiler may easily eliminate the loop completely.

Look at your code from the point of view of clarity and ease of modification. In many cases compiler will do a lot of useful trick related to performance.