直接上程序:
setjmp和longjmp是處理函數嵌套調用的,goto語句不能跨越函數,所以不選擇goto。
#include <setjmp.h>int setjmp(jmp_buf env); //返回值:若第一次直接調用則直接返回0,若從longjmp調用則返回下面的valvoid longjmp(jmp_buf env, int val);
對程序進行不帶優化編譯:
[henry@localhost c]$ gcc -g youhua.c -o youhua
對程序進行帶優化的編譯:
[henry@localhost c]$ gcc -g -O youhua.c -o youhua_after
對比上面結果可以看到,全局、靜態、volatile變量不受優化的影響。
gcc都做了什么優化呢?首先可以看到變量從內存取值優化到從寄存器取值。一下是manual的部分翻譯。
gcc有幾個優化等級:
O0,O1,O2,O3
-O0表示沒有優化,-O1為缺省值,-O3優化級別最高
'-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的多項式和的地址。跳過這些界限可以提高性能,以及由此產生的代碼和數據空間的大小,因此,這些特定的優化被禁用。
完!
參考:·[1]
apue
新聞熱點
疑難解答