[转]gcc优化

http://www.linuxjournal.com/article/7269
http://blog.csdn.net/lanmanck/archive/2010/07/30/5776173.aspx

inline function 必须是  O3 级别的优化

gcc  -O1
       -O2
       -Os
       -O3

Level 1 (-O1)
The purpose of the first level of optimization is to produce an optimized image in a short amount of time. These optimizations typically don't require significant amounts of compile time to complete. Level 1 also has two sometimes conflicting goals. These goals are to reduce the size of the compiled code while increasing its performance. The set of optimizations provided in -O1 support these goals, in most cases. These are shown in Table 1 in the column labeled -O1. The first level of optimization is enabled as:
gcc -O1 -o test test.c

Level 2 (-O2)
The second level of optimization performs all other supported optimizations within the given architecture that do not involve a space-speed trade-off, a balance between the two objectives. For example, loop unrolling and function inlining, which have the effect of increasing code size while also potentially making the code faster, are not performed. The second level is enabled as:
gcc -O2 -o test test.c

Level 2.5 (-Os)
The special optimization level (-Os or size) enables all -O2 optimizations that do not increase code size; it puts the emphasis on size over speed. This includes all second-level optimizations, except for the alignment optimizations. The alignment optimizations skip space to align functions, loops, jumps and labels to an address that is a multiple of a power of two, in an architecture-dependent manner. Skipping to these boundaries can increase performance as well as the size of the resulting code and data spaces; therefore, these particular optimizations are disabled. The size optimization level is enabled as:
gcc -Os -o test test.c

Level 3 (-O3)
The third and highest level enables even more optimizations (Table 1) by putting emphasis on speed over size. This includes optimizations enabled at -O2 and rename-register. The optimization inline-functions also is enabled here, which can increase performance but also can drastically increase the size of the object, depending upon the functions that are inlined. The third level is enabled as:
gcc -O3 -o test test.c

Although -O3 can produce fast code, the increase in the size of the image can have adverse effects on its speed. For example, if the size of the image exceeds the size of the available instruction cache, severe performance penalties can be observed. Therefore, it may be better simply to compile at -O2 to increase the chances that the image fits in the instruction cache.

[转]gcc优化_第1张图片

Any optimization can be enabled outside of any level simply by specifying its name with the -f prefix, as:
gcc -fdefer-pop -o test test.c
We also could enable level 1 optimization and then disable any particular optimization using the -fno- prefix, like this:
gcc -O1 -fno-defer-pop -o test test.c
This command would enable the first level of optimization and then specifically disable the defer-pop optimization.

gcc 命令的常用选项 
选项 解释 
-ansi 只支持 ANSI 标准的 C 语法。这一选项将禁止 GNU C 的某些特色, 
例如 asm 或 typeof 关键词。 
-c 只编译并生成目标文件。 
-DMACRO 以字符串“1”定义 MACRO 宏。 
-DMACRO=DEFN 以字符串“DEFN”定义 MACRO 宏。 
-E 只运行 C 预编译器。 
-g 生成调试信息。GNU 调试器可利用该信息。 
-IDIRECTORY 指定额外的头文件搜索路径DIRECTORY。 
-LDIRECTORY 指定额外的函数库搜索路径DIRECTORY。 
-lLIBRARY 连接时搜索指定的函数库LIBRARY。 
-m486 针对 486 进行代码优化。 
-o FILE 生成指定的输出文件。用在生成可执行文件时。 
-O0 不进行优化处理。 
-O 或 -O1 优化生成代码。 
-O2 进一步优化。 
-O3 比 -O2 更进一步优化,包括 inline 函数。 
-shared 生成共享目标文件。通常用在建立共享库时。 
-static 禁止使用共享连接。 
-UMACRO 取消对 MACRO 宏的定义。 
-w 不生成任何警告信息。 
-Wall 生成所有警告信息。

你可能感兴趣的:(优化,image,gcc,performance,alignment,optimization)