编译器优化
1. 关闭运行时类型检查(turnoff RTTI)
When /GR is on, the compiler defines the _CPPRTTI preprocessor macro. By default, /GR is on. /GR- disables run-time type information.Use /GR if the compiler cannot statically resolve an object type in your code. You usually need the /GR option when your code uses dynamic_cast Operator or typeid. However, /GR increases the size of the .rdata sections of your image. If your code does not use dynamic_cast or typeid, /GR- may produce a smaller image.
#icc 默认是开启RTTI的, 关闭选项:
icc xxx -fno-riit
2. 开启fast floating point calculatinos 或者关闭 strict floating point calculations(如果在精度要求范围内)
gcc/icc xxx -fp-mode fast[=1/2]
默认的fast模式开启的是1,即fat=1, fast=2 能够使程序的浮点运算更快但是要损失更多的精度。
-fp-model precise 会关闭所有会有损计算精度的优化。如果精度要求不高的话关闭此选项。
其他的fp-model选项如下:
参数 | 描述 |
---|---|
precise | Disables optimizations that are not value-safe on floating-point data. |
fast=[1/2] | Enables more aggressive optimizations on floating-point data |
consistent | Enables consistent, reproducible results for different optimization levels or between different processors of the same architecture. |
strict | Enables precise and except, disables contractions, and enables pragma stdc fenv_access. |
source | Rounds intermediate results to source-defined precision. |
double | Rounds intermediate results to 53-bit (double) precision. |
extended | Rounds intermediate results to 64-bit (extended) precision. |
[no-]except | (Linux* and macOS) or except[-] (Windows ) Determines whether strict floating-point exception semantics are honored. |
[注意:]上述是man icc的结果, 貌似linux gcc上没找到-fp-mode fast这么个选项??貌似-ffast-math就是-fp-mode fast=2; 默认是-fp-mode fast=1
3. 开启function level linking
remove unreferenced functions
去掉没有被引用的函数。
#windows:
/Gy
#Linux:
-ffunction-sections
4. 开启assume no pointer aliasing(如果你能确保没有pointer aliasing的话)
#windows:
/Oa
#linux
除了在编译的时候声明之外,还可以在代码中这么声明一个pointer来表明该指针不会被aliasing:
在变量名前添加 __restrict 或者 __restrict __;
5. 不要开启 "frame pointer" (确保没有使用exception)
关闭frame pointer会是程序变快,而且会空出一个寄存器来。参考
gcc manual 是这么说的:
-fomit-frame-pointer
Don't keep the frame pointer in a register for functions that don't need one. This avoids the instructions to save, set up and restore frame pointers; it also makes an extra register available in many functions. It also makes debugging impossible on some machines.
On some machines, such as the VAX, this flag has no effect, because the standard calling sequence automatically handles the frame pointer and nothing is saved by pretending it doesn't exist. The machine-description macro "FRAME_POINTER_REQUIRED" controls whether a target machine supports this flag.
The default setting (when not optimizing for size) for 32-bit GNU/Linux x86 and 32-bit Darwin x86 targets is -fomit-frame-pointer. You can configure GCC with the --enable-frame-pointer configure option to change the default. Enabled at levels -O, -O2, -O3, -Os.
开启方法:
-fomit-frame-pointer
看不懂到底是默认开了还是没开,反正当时候为了保险自己手动加上试试, 记得看看结果对不对
网友评论