本文介绍了耗时:行主要总和列主要总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也碰到过这个问题,我有点糊涂了。两个codeS已经给出,它是问这将需要更多的时间,还是两者都将采取相同的时间。在codeS是:

I have come across this question and I am a bit confused. Two codes have been given and it is asked which would take more time or whether both will take same time. The codes were:

void sumCal(int n,int a[][]){
   int sum=0;
   for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
         sum=sum+a[i][j];
      }
   }
}

void sumCal(int n,int a[][]){
   int sum=0;
   for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
         sum=sum+a[j][i];
      }
   }
}

据我所知,这取决于内存布局,内存是否制定了行的主要方向或列重大明智的。这是正确的答案?

As far as I know it depends on the memory layout, whether the memory is laid out row major wise or column major wise. Is this the right answer?

推荐答案

一个首要的问题确实是矩阵是否是行主或列为主格式布局。由C标准所定义;这是不是就是留给了编译器作者。另一主要问题是该阵列的大小;如果它是小到足以在超高速缓冲存储器,那么它就不是关键的使用哪种机制,但如果过大,以适应在高速缓存中,则一个机制是远远超过其他更快。

One primary issue is indeed whether the matrix is laid out in row-major or column-major format. That is defined by the C standard; it is not something that is left up to compiler writers. The other primary issue is the size of the array; if it is small enough to fit in cache memory, then it isn't critical which mechanism you use, but if it is too big to fit in cache, then one mechanism is vastly quicker than the other.

这是什么呢?那么,对于表明,C使用行优先订单。

Which is it? Well, a quick search on Wikipedia for Row-major Order shows that C uses row-major order.

这篇关于耗时:行主要总和列主要总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 06:59