其中这些二维阵列是有利的是使用,为什么? [英] Which of these two dimensional array are advantageous to use and why?

查看:164
本文介绍了其中这些二维阵列是有利的是使用,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Facebook群组,我看到了这样的问题:

In a facebook group, I saw a question like :

如果某行主导的其中一个优势是,为什么下面的二维数组?

a) for(i=0;i<1000;i++)
      for(j=0;j<1000;j++)
           temp=temp+a[i][j];

b) for(j=0;j<1000;j++)
      for(i=0;i<1000;i++)
         temp=temp+a[i][j]

从我的角度我可以看到在上述两种说法没有什么区别。我想这些都是一样的。

From my point I can see there is no difference in the above two statements. And I guess these both are same.

如果我错了纠正我?

推荐答案

有在理论上是没有什么区别。

There is no difference in theory.

的实际好处是在缓存定位。如果您访问的是相距甚远的位置,可以增加高速缓存未命中的数量,这又使得code运行变慢。

The practical advantage is in cache locality. If you access locations which are far apart, you increase the number of cache misses, which in turn makes the code run slower.

根据您的处理器缓存的大小,你或许会需要以感知的效果与一些较大的合理数量,以取代1000。

Depending on your processor cache size, you would perhaps need to replace 1000 with some reasonably bigger number in order to perceive the effect.

这篇关于其中这些二维阵列是有利的是使用,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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