GCC 编译时优化某一个或几个函数或者不优化某一个或几个函数
生活随笔
收集整理的這篇文章主要介紹了
GCC 编译时优化某一个或几个函数或者不优化某一个或几个函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 有的時候我們會有這樣的需求:不想讓編譯器優化某一個或幾個函數、針對某一個或幾個函數做設置特殊的優化等級。
- 以下有三種方法:
- __attribute((optimize(“STRING”)))的例子,fun1函數使用O0優化級別,fun2函數使用O2優化級別。
- // 首先用__attribute__聲明函數 int fun1(int a, int b) __attribute__((optimize("O0"))); // 然后再定義函數,聲明和定義必須分開,否則編譯錯誤 int fun1(int a, int b) {printf("fun1 is (optimize(\"O0\")"); } int fun2(int a, int b) __attribute__((optimize("O2"))); int fun2(int a, int b) {printf("fun2 is (optimize(\"O2\")"); }
- pragma GCC optimize (“string”…)的例子,pragma語句下面的fun4和fun5函數都使用O3優化級別。pragma語句上面的fun3函數使用命令行指定的優化級別。
- int fun3(int a, int b) {printf("fun3 is optimize default"); } #pragma GCC optimize ("O3") int fun4(int a, int b) {printf("fun4 is (optimize(\"O3\")"); } int fun5(int a, int b) {printf("fun5 is (optimize(\"O3\")"); }
- #pragma GCC push_options #pragma GCC pop_options These pragmas maintain a stack of the current target and optimization options. It is intended for include files where you temporarily want to switch to using a different `#pragma GCC target' or `#pragma GCC optimize' and then to pop back to the previous options. //這些實用程序保留了當前目標和優化選項的堆棧。 它用于包含文件,在這些文件中,您臨時要切換到使用其他“ #pragma GCC目標”或“ #pragma GCC優化”,然后彈出回到先前的選項。 The `#pragma GCC push_options' and `#pragma GCC pop_options' pragmas are not implemented in GCC versions earlier than 4.4.#pragma GCC reset_options This pragma clears the current #pragma GCC target and #pragma GCC optimize to use the default switches as specified on the command line. //該編譯指示清除當前的#pragma GCC目標,并且#pragma GCC優化以使用命令行上指定的默認開關。 The `#pragma GCC reset_options' pragma is not implemented in GCC versions earlier than 4.4.
- #pragma GCC optimize ("string"...) This pragma allows you to set global optimization options for functions defined later in the source file. One or more strings can be specified. Each function that is defined after this point is as if attribute((optimize("STRING"))) was specified for that function. The parenthesis around the options is optional. See Section 6.30 [Function Attributes], page 376, for more information about the optimize attribute and the attribute syntax. The ‘#pragma GCC optimize’ pragma is not implemented in GCC versions earlier than 4.4 ? #pragma GCC push_options #pragma GCC optimize ("O3")//your code optimize ("O3") specially#pragma GCC pop_options
總結
以上是生活随笔為你收集整理的GCC 编译时优化某一个或几个函数或者不优化某一个或几个函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 互联网协议套件(TCP/IP)及七层OS
- 下一篇: 华大单片机HC32L136J8TA读取D