HiveBrain v1.2.0
Get Started
← Back to all entries
principleMinor

Row Major Vs Column Major Order: 2D arrays access in programming languages

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
orderarraysmajorcolumnlanguagesprogrammingrowaccess

Problem

Programmers prefer accessing a 2D array in Row-Major Order rather than Column-Major Order, Why?

Are there some advantages/benefits of accessing a 2D array in row-major as compare to column-major?

Like in C programming, programmers prefer row-major rather than column-major due to the way memory is being allocated to a 2D array when defined statically or dynamically.

Solution

The way you access the array affects performance.

It depends on how the matrix is represented and stored in memory. Often a matrix is stored in row-major order, so that consecutive elements of a row are contiguous in memory. Reading memory in contiguous locations is faster than jumping around among locations. As a result, if the matrix is stored in row-major order, then iterating through its elements sequentially in row-major order may be faster than iterating through its elements in column-major order.

Of course, if the matrix is stored in column-major order, the reverse will be true.

See https://en.wikipedia.org/wiki/Row-_and_column-major_order for more on the subject.

Context

StackExchange Computer Science Q#71985, answer score: 4

Revisions (0)

No revisions yet.