全局变量的性能影响(C,C ++) [英] Global Variables performance effect (c, c++)

查看:125
本文介绍了全局变量的性能影响(C,C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一种非常快速的算法,其中一部分是极快的扫描仪和统计功能. 在这个任务中,我追求任何性能上的好处. 因此,我还对保持代码多线程"友好性感兴趣.

I'm currently developing a very fast algorithm, with one part of it being an extremely fast scanner and statistics function. In this quest, i'm after any performance benefit. Therefore, I'm also interested in keeping the code "multi-thread" friendly.

现在开始提问: 我注意到,将一些非常常访问的变量和数组放入全局"或静态局部"(执行相同的操作)中,具有可衡量的性能优势(在+ 10%的范围内). 我试图理解原因,并找到解决方案,因为我希望避免使用这些类型的分配. 请注意,我认为区别不在于分配",因为在堆栈上分配一些变量和小的数组几乎是瞬时的.我相信区别在于访问"和修改"数据.

Now for the question : i've noticed that putting some very frequently accessed variables and arrays into "Global", or "static local" (which does the same), there is a measurable performance benefit (in the range of +10%). I'm trying to understand why, and to find a solution about it, since i would prefer to avoid using these types of allocation. Note that i don't think the difference comes from "allocation", since allocating a few variables and small array on the stack is almost instantaneous. I believe the difference comes from "accessing" and "modifying" data.

在此搜索中,我从stackoverflow找到了这个旧帖子: 全局变量的C ++性能

In this search, i've found this old post from stackoverflow : C++ performance of global variables

但是我对那里的答案感到非常失望.很少有解释,主要是关于您不应该这样做"(嘿,那不是问题!)和非常粗糙的陈述,例如它不会影响性能",这显然是不正确的,因为我正在精确地对其进行测量.基准工具.

But i'm very disappointed by the answers there. Very little explanation, mostly ranting about "you should not do that" (hey, that's not the question !) and very rough statements like 'it doesn't affect performance', which is obviously incorrect, since i'm measuring it with precise benchmark tools.

如上所述,我正在寻找一种解释,并且,如果有的话,我正在寻找该问题的解决方案.到目前为止,我已经感觉到计算局部(动态)变量的内存地址比全局(或局部静态)变量花费更多.也许有点像ADD运算差异.但这无助于找到解决方案...

As said above, i'm looking for an explanation, and, if it exists, a solution to this issue. So far, i've got the feeling that calculating the memory address of a local (dynamic) variable costs a bit more than a global (or local static). Maybe something like an ADD operation difference. But that doesn't help finding a solution...

推荐答案

这实际上取决于您的编译器,平台和其他详细信息.但是,我可以描述一种全局变量更快的情况.

It really depends on your compiler, platform, and other details. However, I can describe one scenario where global variables are faster.

在许多情况下,全局变量处于固定偏移量.这使生成的指令可以直接直接使用该地址. (类似于MOV AX,[MyVar]的内容.)

In many cases, a global variable is at a fixed offset. This allows the generated instructions to simply use that address directly. (Something along the lines of MOV AX,[MyVar].)

但是,如果您有一个相对于当前堆栈指针或某个类或数组的成员的变量,则需要一些数学运算才能获取数组的地址并确定实际变量的地址.

However, if you have a variable that's relative to the current stack pointer or a member of a class or array, some math is required to take the address of the array and determine the address of the actual variable.

很显然,如果您需要在全局变量上放置某种互斥体以使其保持线程安全,那么几乎肯定会失去任何性能提升.

Obviously, if you need to place some sort of mutex on your global variable in order to keep it thread-safe, then you'll almost certainly more than lose any performance gain.

这篇关于全局变量的性能影响(C,C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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