矩阵的逐行与逐列遍历
遍历矩阵的两种常见方式是行优先和列优先。
行主要顺序:当矩阵被逐行访问时。
列主要顺序:当矩阵被逐列访问时。
例子:
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
区别:如果我们根据时间复杂度来看,两者都会导致O(n 2 ) ,但是当涉及到缓存级别时,其中一个命令的访问速度会比其他命令更快。这取决于我们使用的语言。像在C中一样,以行主要形式存储矩阵,因此在访问 i th之后的第 i+1个元素时,很可能会导致命中,这将进一步减少程序时间。
以下代码显示了行主要和列主要访问的时间差异。
C++
// C program showing time difference
// in row major and column major access
#include
#include
// taking MAX 10000 so that time difference
// can be shown
#define MAX 10000
int arr[MAX][MAX] = {0};
void rowMajor() {
int i, j;
// accessing element row wise
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
arr[i][j]++;
}
}
}
void colMajor() {
int i, j;
// accessing element column wise
for (i = 0; i < MAX; i++) {
for (j = 0; j < MAX; j++) {
arr[j][i]++;
}
}
}
// driver code
int main() {
int i, j;
// Time taken by row major order
clock_t t = clock();
rowMajor();
t = clock() - t;
printf("Row major access time :%f s\n",
t / (float)CLOCKS_PER_SEC);
// Time taken by column major order
t = clock();
colMajor();
t = clock() - t;
printf("Column major access time :%f s\n",
t / (float)CLOCKS_PER_SEC);
return 0;
}
Java
// Java program showing time difference
// in row major and column major access
import java.time.Duration;
import java.time.Instant;
import java.util.*;
class GFG
{
// taking MAX 10000 so that time difference
// can be shown
static int MAX= 10000;
static int [][]arr = new int[MAX][MAX];
static void rowMajor()
{
int i, j;
// accessing element row wise
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX; j++)
{
arr[i][j]++;
}
}
}
static void colMajor()
{
int i, j;
// accessing element column wise
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX; j++)
{
arr[j][i]++;
}
}
}
// Driver code
public static void main(String[] args)
{
// Time taken by row major order
Instant start = Instant.now();
rowMajor();
Instant end = Instant.now();
System.out.println("Row major access time : "+
Duration.between(start, end));
// Time taken by column major order
start = Instant.now();
colMajor();
end = Instant.now();
System.out.printf("Column major access time : "+
Duration.between(start, end));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program showing time difference
# in row major and column major access
# taking MAX 10000 so that time difference
# can be shown
MAX = 1000
from time import clock
arr = [[ 0 for i in range(MAX)] for i in range(MAX)]
def rowMajor():
global arr
# accessing element row wise
for i in range(MAX):
for j in range(MAX):
arr[i][j] += 1
def colMajor():
global arr
# accessing element column wise
for i in range(MAX):
for j in range(MAX):
arr[j][i] += 1
# Driver code
if __name__ == '__main__':
# Time taken by row major order
t = clock()
rowMajor();
t = clock() - t
print("Row major access time :{:.2f} s".format(t))
# Time taken by column major order
t = clock()
colMajor()
t = clock() - t
print("Column major access time :{:.2f} s".format(t))
# This code is contributed by mohit kumar 29
C#
// C# program showing time difference
// in row major and column major access
using System;
using static System.DateTime;
public class GFG
{
// taking MAX 10000 so that time difference
// can be shown
public static int MAX = 1000;
public static int[,] arr = new int[GFG.MAX,GFG.MAX];
public static void rowMajor()
{
int i;
int j;
// accessing element row wise
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;
// accessing element column wise
for (
i = 0; i < GFG.MAX; i++)
{
for (
j = 0; j < GFG.MAX; j++)
{
GFG.arr[j,i]++;
}
}
}
// Driver code
public static void Main(String[] args)
{
// Time taken by row major order
var start = DateTime.UtcNow;
GFG.rowMajor();
var end = DateTime.UtcNow;
TimeSpan spanR = end.Subtract( start );
Console.WriteLine("Row major access time : " + spanR.TotalMinutes + " min");
// Time taken by column major order
start = DateTime.UtcNow;
GFG.colMajor();
end = DateTime.UtcNow;
TimeSpan spanC = end.Subtract( start );
Console.WriteLine("Column major access time : " + spanC.TotalMinutes + " min");
}
}
// This code is contributed by yoursthek2002
输出:
Row major access time :0.492000 s
Column major access time :1.621000 s