[英]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.