什么样的优化确实在C / C ++常量报价? [英] What kind of optimization does const offer in C/C++?

查看:109
本文介绍了什么样的优化确实在C / C ++常量报价?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在哪里可以通过引用或指针为可读性和优化的原因,各地传递参数时,你应该使用const关键字。现在是什么样的优化可以,如果我指定一个说法是恒定的编译器吗?

I know that where possible you should use the const keyword when passing parameters around by reference or by pointer for both readability and optimization reasons. Now what kind of optimizations can the compiler do if I specify that an argument is constant?

有可能是几例:

功能参数:

恒参考:

void foo(const SomeClass& obj)

恒SomeClass的对象:

Constant SomeClass object:

void foo(const SomeClass* pObj)

和常量指针SomeClass的:

And constant pointer to SomeClass:

void foo(SomeClass* const pObj)

变量声明:

const int i = 1234

函数声明:

const char* foo()

什么样的​​编译器优化的每一个机会(如果有的话)?

What kind of compiler optimizations each one offers (if any)?

推荐答案

[读者请注意,大多数这个帖子是从香草萨特的文章举 - 的 http://www.gotw.ca/gotw/081.htm - 而不会被OP属性]

[Readers please note that the majority of this post is lifted from an article by Herb Sutter - http://www.gotw.ca/gotw/081.htm - without attribution by the OP.]

CASE_1: -

当您在程序中声明一个常量,

When you declare a const in your program,

int const x = 2;

编译器可以通过该变量没有提供存储,而在符号表中添加优化掉这个常量。因此,后续的读取只需要间接到符号表,而不是指令,从内存中获取价值。

Compiler can optimize away this const by not providing storage to this variable rather add it in symbol table. So, subsequent read just need indirection into the symbol table rather than instructions to fetch value from memory.

请注意: - 如果你做这样的事情如下: -

NOTE:- If you do something like below:-

const int x = 1;
const int* y = &x;

那么这将迫使编译器为分配空间的x。因此,优化的那种程度是不可能的这种情况。

Then this would force compiler to allocate space for 'x'. So, that degree of optimization is not possible for this case.

在功能参数常量条款意味着参数未在函数修改。据我所知,有使用常量相反,它是确保正确性的方法没有实质性的性能提升。

In terms of function parameters const means that parameter is not modified in the function. As far as I know, there's no substantial performance gain for using const rather it's a means to ensure correctness.

CASE_2: -

难道声明参数和/或返回值作为常量的帮助编译器生成更优化的code吗?

"Does declaring the parameter and/or the return value as const help the compiler to generate more optimal code?"

  const Y& f( const X& x )
  {
    // ... do something with x and find a Y object ...
    return someY;
  }

疑问句=>什么可以在编译器做的更好?

=>它可以避免参数的副本或返回值?

没有,作为参数已经被引用传递的。

No, as argument is already passed by reference.

=>莫非把x或someY的副本到只读存储器?

没有,因为x和someY住它的范围之内,来自和/或给外面的世界。即使someY是动态上f()的自身内的飞分配,它和它的所有权给予到呼叫者

No, as both x and someY live outside its scope and come from and/or are given to the outside world. Even if someY is dynamically allocated on the fly within f() itself, it and its ownership are given up to the caller.

疑问句=>什么关于f的体内出现code可能的优化()?由于常量的,可以在某种程度上编译器提高了code将其生成f的身体()?

甚至当你调用一个const成员函数,编译器不能假设对象x的位或对象someY不会改变。此外,还有一些其他问题(除非编译器进行全局优化)编译器也可能不知道肯定没有其他code可能有别名相同的对象为x和/或someY一个非const引用,是否为相同的对象的任何此类非const引用可能会得到f的执行期间顺便用();而编译器可能甚至不知道是否真实的物体,其中x和someY仅仅是参考,实际上是宣布在首位常量。

Even when you call a const member function, the compiler can't assume that the bits of object x or object someY won't be changed. Further, there are additional problems (unless the compiler performs global optimization): The compiler also may not know for sure that no other code might have a non-const reference that aliases the same object as x and/or someY, and whether any such non-const references to the same object might get used incidentally during the execution of f(); and the compiler may not even know whether the real objects, to which x and someY are merely references, were actually declared const in the first place.

CASE_3: -

  void f( const Z z )
  {
    // ...
  }

疑问句=>会有这个任何优化?

是因为编译器知道是Z确实是一个const对象,它可以即使没有全球性的分析进行一些有益的优化。例如,若f()的体内含有像G的调用(安培; Z),编译器可以肯定的是z的非可变部分不通话时至g改变()

Yes because the compiler knows that z truly is a const object, it could perform some useful optimizations even without global analysis. For example, if the body of f() contains a call like g( &z ), the compiler can be sure that the non-mutable parts of z do not change during the call to g()

这篇关于什么样的优化确实在C / C ++常量报价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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