在GCC编译:-O3有害吗? [英] Compiling in GCC: Is -O3 harmful?

查看:788
本文介绍了在GCC编译:-O3有害吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有人不应该用gcc编译-O3选项。真的吗?如果是这样,避免-O3的原因是什么?

解决方案

答案是:取决于您的代码。



基本的经验法则是这样的:


  • 在-O1处编译器会进行优化,计算时间不会太长。


  • 在-O2编译器执行昂贵的优化,可能会减慢编译过程。它们也可能会使输出程序稍微大一些,但可能并不那么重要。

  • -Os与-O2大致相同,但优化已调整更多的是速度而不是速度。例如,大多数情况下,这两个功能不会发生冲突(更优化的代码执行的步骤更少,因此也更小),但是有一些技巧会重复代码以避免分支处罚(penalty)。

  • $ b
  • 在-O3处,编译器真正开启了空间优化的优化。它会更积极地内联函数,并尝试在可能的情况下使用向量化。




您可以在 GCC文档。如果你真的想超级优化你的代码,那么你可以尝试启用甚至更多的选项,即使在-O3下也不会使用;例如` -floop - * 选项。



速度空间优化的问题,它们会对你的内存缓存的有效性产生负面影响。代码对于CPU来说可能更好,但如果它对你的内存来说不是更好,那么你输了。因此,如果你的程序没有一个单独的热点,那么你可能会发现它的整体速度会变慢。

真实世界优化是一种不精确的科学,原因有三:


  1. 用户的硬件差异很大。 $ b

  2. 对于一个代码库有什么好处可能对另一个代码库不好。

  3. 我们希望编译器能够快速运行,所以它必须做出最好的猜测,而不是尝试所有的选择,并选择最好的。


    基本上,答案总是如果性能很重要尝试所有优化级别,衡量代码的执行情况,并为您选择最合适的。如果性能无关紧要,-O2是您的选择。


    I have heard that one should not compile with -O3 option with gcc. Is that true? If so, what are the reasons for avoiding -O3?

    解决方案

    The answer is: it depends on your code.

    The basic rule of thumb is like this:

    • At -O1 the compiler does optimizations that don't take too long to compute.

    • At -O2 the compiler does "expensive" optimizations that may slow the compile process. They might also make the output program a little larger, but probably not so much.

    • -Os is roughly the same as -O2, but the optimizations are tuned more towards size than speed. For the most part these two features don't conflict (more optimal code does less steps and is therefore smaller), but there are some tricks that duplicate code to avoid branching penalties, for example.

    • At -O3 the compiler really cranks up the space-hungry optimizations. It will inline functions much more aggressively, and try to use vectorization where possible.

    You can read more details in the GCC documentation. If you really want to super optimize your code then you can try to enable even more options not used even at -O3; the -floop-* options, for instance`.

    The problem with speed-space optimizations, in particular, is that they can have a negative impact on the effectiveness of your memory caches. The code might be better for the CPU, but if it's not better for your memory, then you lose. For this reason, if your program doesn't have a single hot-spot where it spends all it's time then you might find it is slowed down overall.

    Real-world optimization is a imprecise science for three reasons:

    1. User's hardware varies a lot.

    2. What's good for one code base might not be good for another.

    3. We want the compiler to run quickly, so it must make best guesses, rather than trying all the options and picking the best.

    Basically, the answer is always, if performance matters try all the optimization levels, measure how well your code performs, and choose the best one for you. And do this again every something big changes.

    If performance does not matter, -O2 is the choice for you.

    这篇关于在GCC编译:-O3有害吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆