直接上程序:html
![](http://static.javashuo.com/static/loading.gif)
setjmp和longjmp是處理函數嵌套調用的,goto語句不能跨越函數,因此不選擇goto。dom
#include <setjmp.h>
int setjmp(jmp_buf env); //返回值:若第一次直接調用則直接返回0,若從longjmp調用則返回下面的val
void longjmp(jmp_buf env, int val);
對程序進行不帶優化編譯:函數
[henry@localhost c]$ gcc -g youhua.c -o youhua
![](http://static.javashuo.com/static/loading.gif)
對程序進行帶優化的編譯:oop
[henry@localhost c]$ gcc -g -O youhua.c -o youhua_after
![](http://static.javashuo.com/static/loading.gif)
對比上面結果能夠看到,全局、靜態、volatile變量不受優化的影響。性能
- 不進行優化時,上面定義的5個變量包括register變量都直接從內存中取值。
- 進行優化後,register變量和局部變量gcc都是從寄存器中取的值。
gcc都作了什麼優化呢?首先能夠看到變量從內存取值優化到從寄存器取值。一下是manual的部分翻譯。優化
gcc有幾個優化等級:ui
O0,O1,O2,O3this
-O0表示沒有優化,-O1爲缺省值,-O3優化級別最高spa
'-O '
'-O1 '
Optimize. Optimizing compilation takes somewhat more time, and a
lot more memory for a large function.
With `-O ', the compiler tries to reduce code size and execution
time, without performing any optimizations that take a great deal ##編譯器試着減小代碼段的大小和代碼執行時間,若是沒有執行一些
of compilation time. 優化結果將花費大量編譯時間。
`-O ' turns on the following optimization flags:
-fdefer-pop 延遲到必要時在函數棧種pop參數
-fdelayed-branch
-fguess-branch-probability
-fcprop-registers
-floop-optimize
-fif-conversion
-fif-conversion2
-ftree-ccp
-ftree-dce
-ftree-dominator-opts
-ftree-dse
-ftree-ter
-ftree-lrs
-ftree-sra
-ftree-copyrename
-ftree-fre
-ftree-ch
-funit-at-a-time
-fmerge-constants
`-O ' also turns on `-fomit-frame-pointer ' on machines where doing ## ’-O‘也打開-fomit-frame-pointer標誌當機器 so does not interfere with debugging. 這樣作不會影響干涉調試。
`-O ' doesn 't turn on `-ftree-sra ' for the Ada compiler. This
option must be explicitly specified on the command line to be enabled for the Ada compiler.`-O2 '
`-O2 '
Optimize even more. GCC performs nearly all supported optimizations that do not involve a space-speed tradeoff.The compiler does not perform loop unrolling or function inlining when you specify `-O2 '.As compared to `-O ',this option increases both compilation time and the performance of the generated code.
進一步的優化。GCC會支持全部不涉及時間空間交換的全部支持的優化選項。當你加入-o2選項時,編譯器不會進行循環展開和函數內聯。與-O選項相比,這個選項會增長編輯時間和合成碼的性能。
`-O2' turns on all optimization flags specified by `-O'. It also turns on the following optimization flags:
-fthread-jumps
-fcrossjumping
-foptimize-sibling-calls
-fcse-follow-jumps
-fcse-skip-blocks
-fgcse
-fgcse-lm
-fexpensive-optimizations
-fstrength-reduce
-frerun-cse-after-loop
-frerun-loop-opt
-fcaller-saves
-fpeephole2
-fschedule-insns
-fschedule-insns2
-fsched-interblock
-fsched-spec
-fregmove
-fstrict-aliasing
-fdelete-null-pointer-checks
-freorder-blocks
-freorder-functions
-falign-functions
-falign-jumps
-falign-loops
-falign-labels
-ftree-vrp
-ftree-pre
Please note the warning under `-fgcse' about invoking `-O2' on programs that use computed gotos.
`-O3'
Optimize yet more.`-O3 ' turns on all optimizations specified by `-O2' and also turns on the `-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' options.
再一次的優化,-O3選項會添加全部-O2中添加的選項,而且添加`-finline-functions ',`-funswitch-loops' and `-fgcse-after-reload' 這三個選項
`-O0'
Do not optimize.This is the default.
-Os至關於-O2.5。是使用了全部-O2的優化選項,但又不縮減代碼尺寸的方法。
詳細的說明以下:
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:
-Os這個特殊的優化等級,可以實現-O2的所有不增長代碼段大小優化,他強調程序的大小而不是程序的運行速度,他包含了全部第二等級的優化,除了對齊優化,這些對齊優化在體系結構的依賴性的程序中,跳過一些線性結構,循環,跳轉和標籤的空間,到一個指數爲2的多項式和的地址。跳過這些界限能夠提升性能,以及由此產生的代碼和數據空間的大小,所以,這些特定的優化被禁用。
完!.net
參考:·[1]
apue