简体   繁体   English

为什么在 for 循环中引用全局变量要慢得多?

[英]Why it is significantly slower to reference global variables in a for loop?

I was reading a book which says to use local variables to eliminate unnecessary memory references.我正在读一本书,上面说使用局部变量来消除不必要的内存引用。 For example, the code below is not very efficient:例如,下面的代码效率不高:

int gsum;  //global sum variable
void foo(int num) {
    for (int i = 0; i < num; i++) {
       gsum += i;
    }
}

It is more efficient to have the code below:使用以下代码效率更高:

void foo(int num) {
    int fsum;
    for (int i = 0; i < num; i++) {
       fsum += i;
    }
    gsum = fsum;
}

I know the second case uses a local variable which is stored in a register.我知道第二种情况使用存储在寄存器中的局部变量。 That's why it is a little bit faster while, in the first case, gsum has to be retrieved from main memory too many times.这就是为什么它要快一点,而在第一种情况下, gsum必须从主内存中检索太多次。

But I still have questions:但我还有疑问:

Q1- Isn't the the gcc compiler smart enough to detect it and implicitly use a register to store the global variable so that subsequent references will use the register exactly as the second case? Q1- gcc 编译器是否足够聪明来检测它并隐式使用寄存器来存储全局变量,以便后续引用将完全像第二种情况一样使用寄存器?

Q2- If, for some reason, the compiler is not able to optimize, then we still have the cache. Q2- 如果由于某种原因编译器无法优化,那么我们仍然有缓存。 Referencing a global variable from the cache is still very fast but I see that some programs which use local variables are 10 times faster than the ones who reference global variables.从缓存中引用全局变量仍然非常快,但我看到一些使用局部变量的程序比引用全局变量的程序快 10 倍。 Why is this?为什么是这样?

Q1: That register is probably going to be needed by other functions which will be called between subsequent calls to foo . Q1:其他函数可能会需要该寄存器,这些函数将在后续调用foo之间调用。 That means gsum will need to be shuttled in and out of the register whenever this function is called.这意味着每当调用此函数时, gsum都需要在寄存器中进出。

Q2: It's possible that the page containing gsum will stay in cache for a while. Q2:包含gsum的页面可能会在缓存中停留一段时间。 However, depending on what else your computer is doing, that page may get written to swap space in order to make room in memory for other pages.但是,根据您的计算机正在执行的其他操作,该页面可能会被写入交换空间,以便为其他页面腾出内存空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “按引用调用”和全局变量之间的区别 - Difference between “call by reference” and global variables 为什么全局变量存储在堆中? - Why global variables are stored in heap? 为什么这个顺序数组循环比使用“查找”数组的循环慢? - Why is this sequential array loop slower than a loop that uses a “lookup” array? 为什么同一个程序中同一个C循环的相同副本需要大量但始终不同的执行时间? - Why would identical copies of the same C loop in the same program take significantly but consistently different times to execute? 当 Array 达到一定大小时,OpenMP 会明显变慢 - OpenMP gets significantly slower when Array reaches certain size 蒙特卡罗模拟运行速度明显慢于顺序 - Monte Carlo simulation runs significantly slower than sequential 为什么将全局变量和静态变量初始化为默认值? - Why are global and static variables initialized to their default values? 为什么不能将注册变量设为全局变量? - Why cant register variables be made global? 为什么代码以线性方式运行比循环运行慢? - Why does code run slower in linear fashion than in a loop? 为什么嵌套for循环比展开相同代码要慢得多? - Why is this nested for loop so much slower than unrolling the same code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM