Row-wise vs column-wise traversal of matrix
Last Updated :
21 Apr, 2023
Two common ways of traversing a matrix are row-major-order and column-major-order.
Row Major Order: When matrix is accessed row by row.
Column Major Order: When matrix is accessed column by column.
Examples:
Input : mat[][] = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}}
Output : Row-wise: 1 2 3 4 5 6 7 8 9
Col-wise : 1 4 7 2 5 8 3 6 9
Difference: If we see according to time complexity, both lead to O(n2), but when it comes to cache level one of the orders access will be faster as compare to other one. It depends on the language we are using. Like in C, store matrix in row major form so while accessing the i+1th element after ith, most probably it will lead to a hit, which will further reduce the time of program.
The following codes are showing the time difference in row major and column major access.
C++
#include <stdio.h>
#include <time.h>
#define MAX 10000
int arr[MAX][MAX] = { 0 };
void rowMajor()
{
int i, j;
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
arr[i][j]++;
}
}
}
void colMajor()
{
int i, j;
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
arr[j][i]++;
}
}
}
int main()
{
int i, j;
clock_t t = clock ();
rowMajor();
t = clock () - t;
printf ( "Row major access time :%f s\n" ,
t / ( float )CLOCKS_PER_SEC);
t = clock ();
colMajor();
t = clock () - t;
printf ( "Column major access time :%f s\n" ,
t / ( float )CLOCKS_PER_SEC);
return 0;
}
|
Java
import java.time.Duration;
import java.time.Instant;
import java.util.*;
class GFG {
static int MAX = 10000 ;
static int [][] arr = new int [MAX][MAX];
static void rowMajor()
{
int i, j;
for (i = 0 ; i < MAX; i++) {
for (j = 0 ; j < MAX; j++) {
arr[i][j]++;
}
}
}
static void colMajor()
{
int i, j;
for (i = 0 ; i < MAX; i++) {
for (j = 0 ; j < MAX; j++) {
arr[j][i]++;
}
}
}
public static void main(String[] args)
{
Instant start = Instant.now();
rowMajor();
Instant end = Instant.now();
System.out.println( "Row major access time : "
+ Duration.between(start, end));
start = Instant.now();
colMajor();
end = Instant.now();
System.out.printf( "Column major access time : "
+ Duration.between(start, end));
}
}
|
Python3
from time import perf_counter
MAX = 1000
arr = [[ 0 for i in range ( MAX )] for i in range ( MAX )]
def rowMajor():
global arr
for i in range ( MAX ):
for j in range ( MAX ):
arr[i][j] + = 1
def colMajor():
global arr
for i in range ( MAX ):
for j in range ( MAX ):
arr[j][i] + = 1
if __name__ = = '__main__' :
t = perf_counter()
rowMajor()
t = perf_counter() - t
print ( "Row major access time :{:.2f} s" . format (t))
t = perf_counter()
colMajor()
t = perf_counter() - t
print ( "Column major access time :{:.2f} s" . format (t))
|
C#
using System;
using static System.DateTime;
public class GFG {
public static int MAX = 1000;
public static int [, ] arr = new int [GFG.MAX, GFG.MAX];
public static void rowMajor()
{
int i;
int j;
for (i = 0; i < GFG.MAX; i++) {
for (j = 0; j < GFG.MAX; j++) {
GFG.arr[i, j]++;
}
}
}
public static void colMajor()
{
int i;
int j;
for (i = 0; i < GFG.MAX; i++) {
for (j = 0; j < GFG.MAX; j++) {
GFG.arr[j, i]++;
}
}
}
public static void Main(String[] args)
{
var start = DateTime.UtcNow;
GFG.rowMajor();
var end = DateTime.UtcNow;
TimeSpan spanR = end.Subtract(start);
Console.WriteLine( "Row major access time : "
+ spanR.TotalMinutes + " min" );
start = DateTime.UtcNow;
GFG.colMajor();
end = DateTime.UtcNow;
TimeSpan spanC = end.Subtract(start);
Console.WriteLine( "Column major access time : "
+ spanC.TotalMinutes + " min" );
}
}
|
Javascript
let MAX = 1000
function rowMajor() {
let i, j;
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
arr[i][j]++;
}
}
}
function colMajor() {
let i, j;
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
arr[j][i]++;
}
}
}
var arr = new Array(MAX);
for (let i = 0; i < MAX; i++)
arr[i] = new Array(MAX);
let start, end;
start = Date.now();
rowMajor();
end = Date.now();
console.log(`Row major access time: ${end - start} ms`);
start = Date.now();
colMajor();
end = Date.now();
console.log(`Col major access time: ${end - start} ms`);
|
Output:
Row major access time :0.492000 s
Column major access time :1.621000 s
Time Complexity: O(MAX*MAX)
Auxiliary Space: O(MAX*MAX)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...