这两个用于循环是更有效的时间和高速缓存性能方面 [英] Which of these two for loops is more efficient in terms of time and cache performance

查看:100
本文介绍了这两个用于循环是更有效的时间和高速缓存性能方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中code以下的样品是缓存性能方面更有效率?为什么呢?

  INT [100] [100];对于(i = 0; I< 100;我++)
{
   为(J = 0; J&小于100; J ++)
   {
       一个[I] [J] = 10;
   }
}

 为(i = 0; I< 100;我++)
{
   为(J = 0; J&小于100; J ++)
   {
      一个[j]的[I] = 10;
   }
}


解决方案

第一种方法是稍微好一点,因为细胞被分配彼此相邻,以奠定。

第一种方法:

  [] [] [] [] [] ...
^第一个任务
   ^第二个任务
[] [] [] [] [] ...
^ 101任务

方法二:

  [] [] [] [] [] ...
^第一个任务
   ^ 101任务
[] [] [] [] [] ...
^第二个任务

Which of the following samples of code is more efficient in terms of cache performance? Why?

int a[100][100];

for(i=0; i<100; i++)
{
   for(j=0; j<100; j++)
   {
       a[i][j] = 10;    
   }
}

or

for(i=0; i<100; i++)
{
   for(j=0; j<100; j++)
   {
      a[j][i] = 10;    
   }
}

解决方案

The first method is slightly better, as the cells being assigned to lays next to each other.

First method:

[ ][ ][ ][ ][ ] ....
^1st assignment
   ^2nd assignment
[ ][ ][ ][ ][ ] ....
^101st assignment

Second method:

[ ][ ][ ][ ][ ] ....
^1st assignment
   ^101st assignment
[ ][ ][ ][ ][ ] ....
^2nd assignment

这篇关于这两个用于循环是更有效的时间和高速缓存性能方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆