相关疑难解决方法(0)

在迭代2D数组时,为什么循环的顺序会影响性能?

可能重复:
这两个for循环中的哪一个在时间和缓存性能方面更有效

下面是两个几乎相同的程序,除了我切换ij变量.它们都运行在不同的时间.有人能解释为什么会这样吗?

版本1

#include <stdio.h>
#include <stdlib.h>

main () {
  int i,j;
  static int x[4000][4000];
  for (i = 0; i < 4000; i++) {
    for (j = 0; j < 4000; j++) {
      x[j][i] = i + j; }
  }
}
Run Code Online (Sandbox Code Playgroud)

版本2

#include <stdio.h>
#include <stdlib.h>

main () {
  int i,j;
  static int x[4000][4000];
  for (j = 0; j < 4000; j++) {
     for (i = 0; i < 4000; i++) {
       x[j][i] = i …
Run Code Online (Sandbox Code Playgroud)

c optimization performance for-loop cpu-cache

350
推荐指数
6
解决办法
4万
查看次数

用于迭代2D数组的嵌套循环的哪种排序更有效

在时间(缓存性能)方面,嵌套循环在迭代2D阵列中的哪一个排序更有效?为什么?

int a[100][100];

for(i=0; i<100; i++)
{
   for(j=0; j<100; j++)
   {
       a[i][j] = 10;    
   }
}
Run Code Online (Sandbox Code Playgroud)

要么

for(i=0; i<100; i++)
{
   for(j=0; j<100; j++)
   {
      a[j][i] = 10;    
   }
}
Run Code Online (Sandbox Code Playgroud)

c performance for-loop cpu-cache

72
推荐指数
5
解决办法
7078
查看次数

标签 统计

c ×2

cpu-cache ×2

for-loop ×2

performance ×2

optimization ×1