不同版本的编译器(例如 GCC)会产生不同的性能吗?

Do different versions of compilers (e.g GCC) generate different performance?

我有一个问题很久了,即新版本的 C/C++ 编译器是否生成更好的代码和更好的性能(例如 G++ 7.3 vs G++ 4.8)?

如果有,加速的来源是什么?如果没有,是否建议更新编译器?

是的,较新版本的 GCC 生成更好的代码并具有更好的性能。

加速来自写入 GCC 的更好的代码生成算法。

如果没有兼容性问题,我建议升级 GCC。较新的 GCC 版本错误较少并生成更好的代码。

如果升级 GCC,您可能还需要升级 Binutils。


只是说明一下,这可能不适用于任何 Microsoft 产品(请参阅评论)。因为我对他们没有任何经验,所以我不知道。然而,总的来说,GCC 的每个版本都有更少的错误和更好的代码,这就是为什么我写下我所做的。

这里有一个关于 GCC 的简短回答——他们的 home website.

上有大量不同的基准测试结果。

例如,通过 Charles Leggett 查看 OOPACK 基准的特定 运行:

The OOPACK kernels consist of 4 programs to measure the relative performance of C++ compilers vs C compilers for abstract data types. The kernels are constructed in such a way that they can be coded in C or C++. The C programs are compiled by the C++ compiler.

The kernels consist of:

  • Max measures how well a compiler inlines a simple conditional.
  • Matrix measures how well a compiler propagates constants and hoists simple invariants.
  • Iterator measures how well a compiler inlines short-lived small objects.
  • Complex measures how well a compiler eliminates temporaries.

其中一个结论是:

gcc optimized C has somewhat improved between 2.91.66 and 3.x

不出所料,快速浏览一下其他一些基准似乎也支持“越新越好”的说法。

根据 GCC Development Mission Statement 中列出的“设计和开发目标”中的类别,改进的原因属于以下三个之一:

  • New optimizations
  • Improved runtime libraries
  • Various other infrastructure improvements

重要的是要注意其他目标涉及“新语言”和“新目标”——因此新版本的相关性将取决于您的用例。

此外,阅读有关 release criteria 的文章——我会警告不要通过谈论一般的“更好的性能”来误导自己,因为编译器设计有许多权衡取舍:

In contrast to most correctness issues, where nothing short of correct is acceptable, it is reasonable to trade off behavior for code quality and compilation time. For example, it may be acceptable, when compiling with optimization, if the compiler is slower, but generates superior code. It may also be acceptable for the compiler to generate inferior code on some test cases if it generates substantially superior code on other test cases.

因此,尤其是对于利基和性能关键型应用程序,您可能希望比较特定的编译器版本


作为旁注,您可能会发现阅读更多关于他们的 development plan 的内容很有趣,其中包括对版本编号等的解释。