函数后面的const如何优化程序?
我见过一些这样的方法:
void SomeClass::someMethod() const;
这个 const 声明有什么作用,它如何帮助优化程序?
编辑
我看到这个问题的第一部分之前已经被问过...但是,它仍然没有回答第二部分:这将如何优化程序?
I've seen some methods like this:
void SomeClass::someMethod() const;
What does this const declaration do, and how can it help optimize a program?
Edit
I see that the first part of this question has been asked before... BUT, it still doesn't answer the second part: how would this optimize the program?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果编译器知道类实例的字段在 const 成员函数调用中不会被修改,则它不必重新加载在 const 函数调用之前可能保留在寄存器中的任何字段。
这有点参考 讨论中的 C++ 常见问题解答在 const_cast 上。
If the compiler knows that the fields of a class instance are not modified across a const member function call, it doesn't have to reload any fields that it may have kept in registers before the const function call.
This is sort of referred to the in C++ FAQ in the discussion on const_cast.
它告诉编译器该方法对类状态没有影响; 您不能分配给其中的任何内容。 请查看 C++ FAQ Lite 18.10。
It tells the compiler that the method has no effect on the classes state; you can't assign to anything in it. Have a look at the C++ FAQ Lite 18.10.
无论 const 存在与否,为 const 方法生成的 asm 代码都将相同。 const 是编译器的函数,而不是运行时的函数,因此,如果有任何性能提升,我认为编译器优化器可能会使用 const 作为内联或确定可能优化的副作用等操作的提示。 简而言之,优化器可能会有所帮助,但如果该方法一开始就很简单,那么我怀疑优化器生成的代码是否会是任何不同的 const 或没有 const。
这是我使用的一个简单的优化(而不是像 const 这样的碰碰运气的东西),它需要一秒钟但会得到回报。 组织您的类变量,使它们更好地落在缓存行边界上,并将最常访问的变量放在一起。 为此,只需将整数、双精度数、浮点数等放在类变量声明的顶部,并将奇数大小的变量放在底部,如下所示:
The asm code that is generated for the const method will be the same if the const is there or not. const is a function of the compiler not the runtime, so if there are any performance gains I would think that the compilers optimizer might use the const as a hint for things like inlining or determining side effects for a possible optimization. So in short the optimizer might be able to help out a bit, but if the method is straight forward to begin with then I doubt that the code generated from the optimizer would be any different const or no const.
Here's an easy optimization I use (rather than hit and miss things like const) which take a second but pay off. Organize your class variables so that they fall on cache line boundaries a little better, and put your most accessed variables together. To do it just put your ints, doubles, floats, etc. together at the top of your class variable declarations and your odd sized variables at the bottom like so:
它允许您在
const
对象上调用类成员函数:还有用于
易失性
对象的易失性
限定符,您还可以使函数const 易失性
用于const 易失性
对象,但这两个非常罕见。It allows you to call the class member function on
const
objects:There's also the
volatile
qualifier for use withvolatile
objects, and you can also make functionsconst volatile
for use onconst volatile
objects, but those two are exceedingly rare.它防止 someMethod 更改该类的对象的任何成员变量。
It prevents someMethod from altering any member variable of an object of that class.
我关于优化的第一个想法是,由于“const”表示实例的状态没有改变,因此编译器在重新排序对该实例上的方法的附近调用方面可能有更多的自由。
My first thought regarding optimization is that since the 'const' indicates that the instance's state hasn't changed, the compiler possibly has more freedom with respect to reordering nearby calls to methods on that instance.