[英]What exactly happens when -O3 is set
I have the following code : 我有以下代码:
int main()
{
int i=0;
for(i=0;i<10000000;i++)
//do something
return 0;
}
When I run this code using 'gcc -o file file.c' command, it is getting executed in 244 milli seconds and when I execute the same code using 'gcc -O3 file.c -o file' it is getting executed in 0 milli seconds(very little time which is shown as zero). 当我使用'gcc -o file file.c'命令运行此代码时,它在244毫秒内被执行,而当我使用'gcc -O3 file.c -o file'执行相同的代码时,它在0被执行。毫秒(很少的时间显示为零)。 I could not understand what optimization is done by -O3 flag in such a way that we get lot of speed in execution.
我无法理解-O3标志以何种方式进行了优化,以至于我们获得了很大的执行速度。
Any link,doc or an explaination regarding '-O3' optimization(apart from gcc.gnu.org, which I have seen and found the info given there is not sufficient to provide me a decent explaination and insight of what goes on) is much appreciated. 关于'-O3'优化的任何链接,文档或解释(除了gcc.gnu.org,我已经看到并发现给出的信息不足以为我提供像样的解释和对发生的事情的深刻理解)很多赞赏。
Ultimately we need to know what //do something
does. 最终,我们需要知道
//do something
。 You should paste your exact code that you used because technically the return statement would be executed as the body of the loop. 您应该粘贴您使用的确切代码,因为从技术上讲return语句将作为循环的主体执行。
If you had something like this: 如果您有这样的事情:
int main()
{
int i=0;
int junkInteger = 0;
for(i=0; i < 5 ;i++)
{
junkInteger++; // just to have a body
}
return 0;
}
With optimization, your loop may become unrolled like such and would thus cause your code size to become larger. 通过优化,您的循环可能会像这样展开,从而导致您的代码大小变大。
int main()
{
int i=0;
int junkInteger = 0;
// loop gets unrolled
junkInteger++;
junkInteger++;
junkInteger++;
junkInteger++;
junkInteger++;
return 0;
}
When the loop gets unrolled, the low level assembly/machine code does not have to perform a Load, Store, Compare, and Branch to manage the loop. 展开循环时,低级程序集/机器代码不必执行“装入”,“存储”,“比较”和“分支”来管理循环。
You should try your code again with this: 您应该使用以下代码再次尝试您的代码:
int main()
{
int i=0;
for(i=0;i<10000000;i++)
{
//do something
}
return 0;
}
Compile it with both optimization on and off and examine the file sizes and even put in the compile option to generate assembly code. 在打开和关闭优化的情况下对其进行编译,并检查文件大小,甚至将其置于compile选项中以生成汇编代码。 Usually with optimizations enabled, code size can become larger due to loop unrolls.
通常,启用优化后,由于循环展开,代码大小可能会变大。 Although the code could be optimized out since the body does nothing.
尽管由于主体什么都不做,所以可以优化代码。 Perhaps use a volatile loop counter to ensure it is not optimized out.
也许使用易失性循环计数器以确保未对其进行优化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.