Memory layout of multi-dimensional arrays - Eli Bendersky's website
When working with multi-dimensional arrays, one important decision programmers have to make fairly early on in the project is what memory layout to use for storing the data, and how to access such data in the most efficient manner. Since computer memory is inherently linear - a one-dimensional structure, mapping multi-dimensional data on it can be done in several ways. In this article I want to examine this topic in detail, talking about the various memory layouts available and their effect on the performance of the code.
Row-major vs. column-major
By far the two most common memory layouts for multi-dimensional array data are row-major and column-major.
When working with 2D arrays (matrices), row-major vs. column-major are easy to describe. The row-major layout of a matrix puts the first row in contiguous memory, then the second row right after it, then the third, and so on. Column-major layout puts the first column in contiguous memory, then the second, etc.
Higher dimensions are a bit more difficult to visualize, so let's start with some diagrams showing how 2D layouts work.
Read full article from Memory layout of multi-dimensional arrays - Eli Bendersky's website
No comments:
Post a Comment