-4

Now I'm tying to follow the below subject

Performance of 2-dimensional array vs 1-dimensional array

especially, when I assign in my code.cpp code.

actually the below method terribly slow then just mapping

1

int getIndex(int row, int col) return row*NCOLS+col;

#define NROWS 10
#define NCOLS 20
This:

int main(int argc, char *argv[]) {
    int myArr[NROWS*NCOLS];
    for (int i=0; i<NROWS; ++i) {
       for (int j=0; j<NCOLS; ++j) {
          myArr[getIndex(i,j)] = i+j;
       }
    }
    return 0;
}

than

2

#define NROWS 10
#define NCOLS 20
This:

int main(int argc, char *argv[]) {
    int myArr[NROWS*NCOLS];
    for (int i=0; i<NROWS; ++i) {
       for (int j=0; j<NCOLS; ++j) {
          myArr[row*NCOLS+col] = i+j;
       }
    }
    return 0;
}

But i can't understand it why does '1' slower than '2'?

in experimentally, '1' is slower alomst twice time than '2'. I think this is not makes sense.

Community
  • 1
  • 1
amel
  • 1
  • 1

2 Answers2

1

Because you didn't enable optimizations. getIndex() is small enough to be (almost certainly) inlined. Just enabling Release Mode on Visual Studio made the "slow" version so fast that I wasn't able to make the array big enough to measure the time without running into a stack overflow. Accessing an array on the heap would distort the test results, so that's not an option. Apart from that, you didn't use a 2D array in your code, it would look like this: int myArr[NROWS][NCOLS].
Simple math (like i + j) will most likely not be a bottleneck in your code either. If it becomes one, you should look for new algorithms first. For example: do you really need to iterate through the entire array or would other data types, which don't access the array by index, be more fitting? There are very few cases where micro-optimizations like this are really necessary. Probably never, if your array has a size of 10*20 elements.

Go for readability, finish your program, profile it, then deceide if that loop really needs optimization.

CookedCthulhu
  • 744
  • 5
  • 14
0

Cause in first example you use a function and in second example you make it inline. You may know that when a program call a function then it save its current state, where it came back again. For this works it need a little clock cycle.

So according to your code. Your first example use a little bit clock cycle by calling function, than second example. For this reason second one might be faster than first one.

Here you find similar logic: http://www.cplusplus.com/forum/articles/20600/

Jaggesher Mondal
  • 193
  • 2
  • 10
  • You are equating a high-level concept (a function call) with low-level speculation (whether the function's code will be inlined or not). Profiling non-optimized builds is pointless -- and the article you linked is chock full of mistakes. – Quentin May 18 '17 at 10:35