开发者

What is the difference between storing the multi-dimensional arrays in memory in Row Major or Column Major fashion?

What is the difference between st开发者_如何学Coring the multi-dimensional arrays in memory in Row Major or Column Major fashion?

As far as I know, 'C' seems to be following the Row Major style.

Just out of curiosity I would like to know, are there any benefits of one style over another?


In general, you can do emulate each one with the other, so there's no inherent advantage to one over the other. However, cache implementations usually consider locality of reference as a positive factor for estimating whether a memory location is going to get accessed soon. That may have performance implications. For instance, in a row-major implementation, this code snippet:

int sum = 0;
for (int i = 0; i < n; ++i)
  for (int j = 0; j < m; ++j) 
     sum += a[i][j];

is likely to be faster than:

int sum = 0;
for (int i = 0; i < m; ++i)
  for (int j = 0; j < n; ++j) 
     sum += a[j][i];

You should try to design your algorithms so that you code outer loops over rows in a row-major environment and over columns in a column major environment to minimize cache misses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜