Skip to content
Related Articles

Related Articles

Improve Article

Performance analysis of Row major and Column major order of storing arrays in C

  • Difficulty Level : Medium
  • Last Updated : 01 Nov, 2019

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.

Below program illustrates that row major order storing of arrays in C is more efficient than column-major order(though Pascal and Fortran follows column major order):




#include <stdio.h>
#include <time.h>
int m[9999][999];
  
void main()
  
{
    int i, j;
    clock_t start, stop;
    double d = 0.0;
  
    start = clock();
    for (i = 0; i < 9999; 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 < 9999; 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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up
Recommended Articles
Page :