# Performance analysis of Row major and Column major order of iterating arrays in C

In computing, row-major order and column-major order are methods for storing multidimensional arrays in linear storage such as random access memory.
The two mentioned ways differ from each other with respect to the order in which elements are stored contiguously in the memory. The elements in row-major order are arranged consecutively along the row and that in the column-major order are arranged consecutively along the column. While the terms allude to the rows and columns of a two-dimensional array, i.e. a matrix, the orders can be generalized to arrays of any dimension by noting that the terms row-major and column-major are equivalent to lexicographic and lexicographic orders, respectively.
In C (and many other languages like C++, Java etc), 2-D arrays are stored in row-major order.(though Pascal and Fortran follows column major order)

This program DOES NOT illustrates that row major order storing of arrays in C is more efficient than column-major order.

It only shows that in C language, 2-D arrays are stored in row major order and thus iterating its elements in a row major order is more efficient.

In languages like Pascal and Fortran, iterating by column major order will be more efficient because 2-D arrays are stored in column major order there.

The reason for this is explained properly here https://cs.stackexchange.com/questions/71985/row-major-vs-column-major-order-2d-arrays-access-in-programming-languages.

## C

 `#include ` `#include ` `int` `m[999][999];` `//Taking both dimensions same so that while running the loops, ` `//number of operations (comparisons, iterations, initializations) ` `//are exactly the same. Refer this for more` `// https://www.geeksforgeeks.org/a-nested-loop-puzzle/`   `void` `main()`   `{` `    ``int` `i, j;` `    ``clock_t` `start, stop;` `    ``double` `d = 0.0;`   `    ``start = ``clock``();` `    ``for` `(i = 0; i < 999; i++)` `        ``for` `(j = 0; j < 999; j++)` `            ``m[i][j] = m[i][j] + (m[i][j] * m[i][j]);`   `    ``stop = ``clock``();` `    ``d = (``double``)(stop - start) / CLOCKS_PER_SEC;` `    ``printf``(``"The run-time of row major order is %lf\n"``, d);`   `    ``start = ``clock``();` `    ``for` `(j = 0; j < 999; j++)` `        ``for` `(i = 0; i < 999; i++)` `            ``m[i][j] = m[i][j] + (m[i][j] * m[i][j]);`   `    ``stop = ``clock``();` `    ``d = (``double``)(stop - start) / CLOCKS_PER_SEC;` `    ``printf``(``"The run-time of column major order is %lf"``, d);` `}`

Output:

```The run-time of row major order is 0.067300
The run-time of column major order is 0.136622```

Time Complexity: O(999*999), as we are using nested loops to traverse 999*999 times.

Auxiliary Space: O(999*999), as we are  using extra space for matrix.

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next