为什么编译需要这么长时间?

发布于 07-14 08:48 字数 1435 浏览 10 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浪菊怪哟2024-07-21 08:48:46

C++ 的一个特定问题导致其速度极其缓慢,与几乎任何其他语言不同,您无法独立于语义分析来解析它。

One C++ specific problem that makes it horribly slow is that, unlike almost any other language, you can't parse it independently of semantic analysis.

飘过的浮云2024-07-21 08:48:46

编译是一个复杂的过程,涉及相当多的步骤:

  • 扫描/词法
  • 分析
  • 中间代码生成
  • 可能 中间代码优化
  • 目标机器代码生成
  • 可选的机器相关代码优化

(不考虑链接)。

当然,对于较长的程序,这将需要一些时间。

Compiling is a complicated process which involves quite a few steps:

  • Scanning/Lexing
  • Parsing
  • Intermediate code generation
  • Possibly Intermediate code optimization
  • Target Machine code generation
  • Optionally Machine-dependent code optimization

(Leaving aside linking.)

Naturally, this will take some time for longer programs.

绿萝2024-07-21 08:48:46

预编译头文件的速度要快得多,至少从 1988 年起就已经知道了。C

编译器或 C++ 编译器花费很长时间的通常原因是它必须 #include、预处理,然后 lex无数的代币。

作为练习,您可能会发现在典型的头文件集合上运行 cpp 需要多长时间,然后测量 lex 输出需要多长时间。

gcc -O 使用 Chris Fraser 和 Jack Davidson 开发的非常有效但有点慢的优化技术。 大多数其他优化器可能会很慢,因为它们涉及对相当大的数据结构的重复迭代。

Precompiled headers are way faster, as has been known at least since 1988.

The usual reason for a C compiler or C++ compiler to take a long time is that it has to #include, preprocess, and then lex gazillions of tokens.

As an exercise you might find out how long it takes just to run cpp over a typical collection of header files---then measure how long it takes to lex the output.

gcc -O uses a very effective but somewhat slow optimization technique developed by Chris Fraser and Jack Davidson. Most other optimizers can be slow because they involve repeated iteration over fairly large data structures.

月亮邮递员2024-07-21 08:48:46

语言设计确实对编译器性能有影响。 C++ 编译器通常比 C# 编译器慢,这与语言的设计有很大关系。 (这也取决于编译器实现者,Anders Hejlsberg 实现了 C#,并且是最好的之一。 )

C++ 的简单“头文件”结构导致其性能降低,尽管预编译头通常会有所帮助。 C++ 是一种比 C 复杂得多的语言,因此 C 编译器通常速度更快。

Language design does have an effect on compiler performance. C++ compilers are typically slower than C# compilers, which has a lot to do with the design of the language. (This also depends on the compiler implementer, Anders Hejlsberg implemented C# and is one of the best around.)

The simplistic "header file" structure of C++ contributes to its slower performance, although precompiled headers can often help. C++ is a much more complex language than C, and C compilers are therefore typically faster.

甜警司2024-07-21 08:48:46

编译不需要花费很长时间:tcc 编译 ANSI c 的速度足够快,足以发挥作用作为一名口译员。

需要考虑的一些事情:

  1. 扫描和解析过程的复杂性。 据推测,需要长时间的前瞻会带来伤害,上下文(而不是上下文无关)语言也是如此。
  2. 内部代表。 构建和开发一个大型且功能齐全的 AST 需要一些时间。 想必您应该使用最简单的内部表示来支持您想要实现的功能。
  3. 优化。 优化很挑剔。 您需要检查许多不同的条件。 您可能想要进行多次传递。 所有这一切都需要时间。

Compilation does not need to take long: tcc compiles ANSI c fast enough to be useful as an interpreter.

Some thing to think about:

  1. Complexity in the scanning and parsing passes. Presumably requiring long look-aheads will hurt, as will contextual (as opposed to context-free) languages.
  2. Internal representation. Building and working on a large and featureful AST will take some time. Presumably you should use the simplest internal representation that will support the features you want to implement.
  3. Optimization. Optimization is fussy. You need to check for a lot of different conditions. You probably want to make multiple passes. All of this is going to take time.
清音悠歌2024-07-21 08:48:46

它们需要多长的时间,这通常取决于您向编译单元注入了多少无关的东西。 我希望看到您更快地手动编译它们:-)

第一次编译文件时,您应该根本没有标头。 然后根据需要添加它们(并在完成后检查是否仍然需要它们)。

减少该时间的其他方法是保持编译单元较小(在极端情况下甚至达到每个文件一个函数的程度)并使用类似 make 的工具来确保只构建所需的内容。

一些编译器(实际上是 IDE)在后台进行增量编译,以便它们(几乎)总是接近完全编译。

They take as long as they take and it usually depends on how much extraneous stuff you inject into your compilation units. I'd like to see you hand-compile them any faster :-)

The first time you compile a file, you should have no headers at all. Then add them as you need them (and check when you're finished whether you still need them).

Other ways of reducing that time is to keep your compilation units small (even to the point of one function per file, in an extreme case) and use a make-like tool to ensure you only build what's needed.

Some compilers (IDE's really) do incremental compilation in the background so that they're (almost) always close to fully-compiled.

故人的歌2024-07-21 08:48:46

我认为这里的其他答案错过了导致 C++ 编译速度变慢的情况的一些重要部分:

  • .obj/.o 文件保存到磁盘并读回的编译模型,然后链接它们
  • 一般情况下的链接,特别是不良的慢速链接器
  • 过于复杂的宏预处理器
  • 任意复杂的图灵完备模板系统
  • 嵌套并重复包含源文件,即使使用 #pragma Once
  • 用户造成的碎片、分割代码太多的文件(在极端情况下,甚至每个文件一个函数)
  • 编译器中臃肿或省力的内部数据结构
  • 过度膨胀的标准库,模板滥用

相比之下,这些不会减慢 C++ 编译速度:

  • 扫描/ Lexin
  • 解析
  • 中间代码生成
  • 目标机器代码生成

顺便说一句,优化是最大的减慢之一,但从某种程度上来说,它是这里唯一实际必要的减慢,而且它完全是可选的。

I think the other answers here have missed some important parts of the situation that slow C++ compilation:

  • Compilation model that saves .obj/.o files to disk, reads them back, then links them
  • Linking in general and bad slow linkers in particular
  • Overly complex macro preprocessor
  • Arbitrarily complex Turing-complete template system
  • Nested and repeated inclusion of source files, even with #pragma once
  • User-inflicted fragmentation, splitting code into too many files (even to the point of one function per file, in an extreme case)
  • Bloated or low-effort internal data structures in the compiler
  • Overbloated standard library, template abuse

By contrast, these don't slow C++ compilation:

  • Scanning/Lexing
  • Parsing
  • Intermediate code generation
  • Target machine code generation

As an aside, optimization is one of the biggest slowdowns, but it is the only slowdown here that is actually necessary by some measure, and also it is entirely optional.

汹涌人海2024-07-21 08:48:46

运行 Idera RAD Studio(有免费版本)。 它带有 C++ 和 Delphi。 Delphi 代码的编译时间只是执行相同操作的 C++ 代码的一小部分。 这是因为 C++ 在过去几十年里发展得非常可怕,没有太多考虑编译器的后果,因为它是复杂的上下文确定的宏,并且在某种程度上是所谓的“.hpp”地狱。 艾达也有类似的问题。 Pascal 的 Delphi 方言从一开始就被设计为一种高效的编译语言。 因此,编译和运行只需几秒钟,而不是几分钟,从而使迭代调试变得快速而轻松。 调试编译缓慢的语言是一种巨大的时间浪费,而且是一种痛苦,你知道吗! 顺便说一句,Anders 在 M$ 偷走他之前也写过 Delphi!

Run Idera RAD Studio (there is a free version). It comes with C++ and Delphi. The Delphi code compiles in a tiny fraction of the time that C++ code doing the same thing does. This is because C++ evolved horribly over the decades, with not much thought to compiler consiquences for it's complex context determined macros and to some degree the so called ".hpp" hell. Ada has similar issues. Delphi dialect of Pascal was designed from the ground up to be an efficient language to compile. So compiler and run takes seconds, instead of minutes, making iterative debugging fast and easy. Debugging slow to compile languages is a huge time waster, and a pain in the you know what! BTW Anders also wrote Delphi before M$ stole him!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文