Why is iterating 2D array row major faster than column major?

前端 未结 3 1011
别跟我提以往
别跟我提以往 2020-12-01 15:02

Here is simple C++ code that compare iterating 2D array row major with column major.

#include 
#include 

using namespace std;

c         


        
3条回答
  •  孤街浪徒
    2020-12-01 15:35

    Your array is actually a ragged array, so row major isn't entirely a factor.

    You're seeing better performance iterating over columns then rows because the row memory is laid out linearly, which reading sequentially is easy for the cache predictor to predict, and you amortize the pointer dereference to the second dimension since it only needs to be done once per row.

    When you iterate over the rows then columns, you incur a pointer dereference to the second dimension per iteration. So by iterating over rows, you're adding a pointer dereference. Aside from the intrinsic cost, it's bad for cache prediction.

    If you want a true two-dimensional array, laid out in memory using row-major ordering, you would want...

    int A[1000][1000];
    

    This lays out the memory contiguously in row-major order, instead of one array of pointers to arrays (which are not laid out contiguously). Iterating over this array using row-major would still perform faster than iterating column-major because of spatial locality and cache prediction.

提交回复
热议问题