在计算中,行优先级和列优先级是用于在诸如随机存取存储器的线性存储器中存储多维阵列的方法。
提到的两种方式在元素连续存储在内存中的顺序方面彼此不同。以行为主的元素沿行连续排列,以行为主的元素沿列连续排列。尽管术语指的是二维数组(即矩阵)的行和列,但是可以通过注意术语行主要和列主要等价于词典顺序和词典顺序,将阶数推广到任何维数的数组,分别。
下面的程序说明了C中数组的行主要顺序存储比列主要顺序更有效(尽管Pascal和Fortran遵循列主要顺序):
#include
#include
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);
}
输出:
The run-time of row major order is 0.067300
The run-time of column major order is 0.136622
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。