GCC中的优化 [英] Optimization in GCC

查看:163
本文介绍了GCC中的优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题:
$ b $(1)我学到了-O3不推荐用于GCC的地方,因为


-O3优化级别可能会增加生成的可执行文件的速度,但也会增加其大小。在这些优化不利的情况下,这个选项实际上可能会使程序变慢。实际上它不应该用gcc 4.x在系统范围内使用。自3.x版以来,gcc的行为发生了显着变化。在3.x中,-O3已被证明比-O2更快的执行时间,但gcc 4.x不再是这种情况。使用-O3编译所有软件包将导致需要更多内存的较大二进制文件,并且会显着增加编译失败或意外程序行为(包括错误)的几率。缺点大于好处;请记住收益递减的原则。


假设我有一台工作站(Kubuntu9.04),它有128 GB内存和24核心,并由许多用户共享,其中一些用户可能使用60 GB内存运行密集型程序。 (2)我还了解到,当一个正在运行的程序意外崩溃时,任何调试信息总比没有更好,所以,对于我来说,-O2是比-O3更好的选择吗?建议将-g用于开发和部署的优化程序。但是当用-ggdb3和-O2或-O3一起编译时,它会减慢执行速度吗?假设我仍然使用相同的工作站。 用-O2和-O3编译您的应用程序。此外还有一些-O3包含的单独优化选项,您可以单独打开和关闭。关于更大的二进制文件的警告,请注意,仅仅比较使用-O2和-O3编译的可执行文件大小在这里并不会有太大的好处,因为它是这里最重要的小型关键内部循环的大小。您必须进行基准测试。


  • 它会产生更大的可执行文件,但不应该有任何可衡量的减速。



  • I have two questions:

    (1) I learned somewhere that -O3 is not recommended with GCC, because

    The -O3 optimization level may increase the speed of the resulting executable, but can also increase its size. Under some circumstances where these optimizations are not favorable, this option might actually make a program slower. in fact it should not be used system-wide with gcc 4.x. The behavior of gcc has changed significantly since version 3.x. In 3.x, -O3 has been shown to lead to marginally faster execution times over -O2, but this is no longer the case with gcc 4.x. Compiling all your packages with -O3 will result in larger binaries that require more memory, and will significantly increase the odds of compilation failure or unexpected program behavior (including errors). The downsides outweigh the benefits; remember the principle of diminishing returns. Using -O3 is not recommended for gcc 4.x.

    Suppose I have a workstation (Kubuntu9.04) which has 128 GB of memory and 24 cores and is shared by many users, some of whom may run intensive programs using like 60 GB memory. Is -O2 a better choice for me than -O3?

    (2) I also learned that when a running program crashes unexpectedly, any debugging information is better than none, so the use of -g is recommended for optimized programs, both for development and deployment. But when compiled with -ggdb3 together with -O2 or -O3, will it slow down the speed of execution? Assume I am still using the same workstation.

    解决方案

    1. The only way to know for sure is to benchmark your application compiled with -O2 and -O3. Also there are some individual optimization options that -O3 includes and you can turn on and off individually. Concerning the warning about larger binaries, note that just comparing executable file sizes compiled with -O2 and -O3 will not do much good here, because it is the size of small critical internal loops that matters here the most. You really have to benchmark.

    2. It will result in a larger executable, but there shouldn't be any measurable slowdown.

    这篇关于GCC中的优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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