[英]pragma gcc optimize in function
In C code, I want to optimize few 'for' loops in function, but not in whole function.在 C 代码中,我想优化 function 中的几个“for”循环,但不是整个 function。 What I want to do is,我想做的是,
int main(){
for(int a=~~~~~)
for(int b=~~~~~)
I want to optimize first for loop using O3 to compare with second for loop.我想使用 O3 优化第一个 for 循环以与第二个 for 循环进行比较。 However,然而,
#pragma GCC push_options
is not allowed to use in function.不允许在 function 中使用。 Is there any way that I can use optimization in function?有什么方法可以在 function 中使用优化? Thanks!谢谢!
Fairly useful way is to split up a code to functions, eg:相当有用的方法是将代码拆分为函数,例如:
int main() {
for (int i = 0; i < 5; ++i) printf("%d", i);
for (int j = 0; j < 5; ++j) printf("%d", 5 - i);
}
then becomes然后变成
int main() {
fn1();
fn2();
}
inline void fn1() {
for (int i = 0; i < 5; ++i) printf("%d", i);
}
// here you can place pragma
inline void fn2() {
for (int j = 0; j < 5; ++j) printf("%d", 5 - i);
}
Most probably, functions would be inlined and compiler can understand its pragams as according to doc:最有可能的是,函数将被内联,编译器可以根据文档理解它的编译:
#pragma GCC optimize (string, …) #pragma GCC 优化(字符串,...)
This pragma allows you to set global optimization options for functions defined later in the source file .此 pragma 允许您为稍后在源文件中定义的函数设置全局优化选项。
Moreover, I would consider to put these functions to files, so you are able to have a finer-grained flags specific to a compilation unit and do not depend on compiler-specific pragmas (which I believe does not work on clang/inter-compiler/msvc/etc).此外,我会考虑将这些函数放入文件中,这样您就可以拥有特定于编译单元的更细粒度的标志,并且不依赖于编译器特定的编译指示(我相信这不适用于 clang/inter-compiler /msvc/等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.