常量叠算(Constant folding),还是叫常量合并更合适点,呵呵
来源:互联网 发布:ubuntu 14.04 u盘 编辑:程序博客网 时间:2024/05/16 11:28
今天刚学到,以此为记。
先看一个例子:
const int c=111;
int& r=const_cast<int&>(c);
int* p=const_cast<int*>(&c);
cout<<"const [<<&c<<"]"<<c<<endl;
cout<<"refer [<<&r<<"]"<<r<<endl;
cout<<"point [<<p<<"]"<<*p<<endl;
r=222;
cout<<"after:"<<endl;
cout<<"const [<<&c<<"]"<<c<<endl;
cout<<"refer [<<&r<<"]"<<r<<endl;
cout<<"point [<<p<<"]"<<*p<<endl;
答案如下:
const [0xbfff5614]111
refer [0xbfff5614]111
point [0xbfff5614]111
after:
const [0xbfff5614]111
refer [0xbfff5614]222
point [0xbfff5614]222
常量叠算(Constant folding)是一个编译器的优化技巧, 指在编译时就对常量表达式进行预求值。
比如上面小懒虫虫说的const int c = 1;常量折叠并不对a分配存储空间,而是把c = 1;保存到符号表里,
在用到c时,会根据符号表把c替换成1。想要不进行常量折叠,加extern就行了,即extern const int c = 1;不过c没有定义的话,加了extern也不能分配存储空间,还是要进行所谓的常量折叠。
如果整个程序中没有用到常量的地址的话,那么很有可能编译器不会为该常量分配空间。
比如表达式 array[ MAXSIZE + 1] //#define MAXSIZE 10
在编译之后便被当作 array[11],这样有利于提高运行时性能并减少目标代码尺寸。
这种优化通常仅适用于内置的数值,布尔类型。
所以上面的代码实际上变成了
const int c=111;
int& r=const_cast<int&>(c);
cout<<"const [<<&c<<"]"<<111<<endl;
cout<<"refer [<<&r<<"]"<<r<<endl;
cout<<"point [<<p<<"]"<<*p<<endl;
r=222;
cout<<"after:"<<endl;
cout<<"const [<<&c<<"]"<<111<<endl;
cout<<"refer [<<&r<<"]"<<r<<endl;
cout<<"point [<<p<<"]"<<*p<<endl;
像这种表达式也在合并范围之内
for(int i=0;i<100*200*300;i++){
......
}
- 常量叠算(Constant folding),还是叫常量合并更合适点,呵呵
- 常量折叠 Constant folding
- Constant folding 常量折叠
- constant常量
- struts2中的 常量 constant
- 提炼常量 Extract Constant
- Constant Configuration(常量配置)
- struts2常用的常量constant
- 字面常量(literal constant)
- 常用的常量介绍<constant>
- struts2常用的常量constant
- Struts 2 constant 常量配置
- common.constant 常量枚举类型
- struts框架<constant>常量配置
- 编程语言中的常量折叠(const folding)
- 如何在Java中定义常量(Constant)
- struts.xml中配置常量constant详解
- struts.xml 中配置常量constant详解
- The Inventor Mentor-第十五章 拖拽器和操作器
- C++引用
- Java Date decrease or increase
- 千万别入错行 导师送给我的15条人生建议
- 职位:售前工程师(北京)
- 常量叠算(Constant folding),还是叫常量合并更合适点,呵呵
- 人生建议
- 说话艺术12条
- Spring2.0配置文件加载util标签的Schema
- 诊断 Java 代码: 轻松掌握 Java 泛型
- 设置IE每次访问页面是检查所存网页的较新版本
- 类似google提示窗口
- 打印
- 新编日语-第6课 読解文