- 有的时候我们会有这样的需求:不想让编译器优化某一个或几个函数、针对某一个或几个函数做设置特殊的优化等级。
- 以下有三种方法:
- __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 编译时优化某一个或几个函数或者不优化某一个或几个函数
最新推荐文章于 2023-11-29 22:46:45 发布