为什么生成的二进制文件这么大?
为什么编译 C++ 程序时生成的二进制文件如此之大(轻松达到源代码文件大小的 10 倍)?与不需要这种编译的解释语言相比(因此程序大小只是代码文件的大小),这有什么优点?
Why are the binaries that are generated when I compile my C++ programs so large (as in easily 10 times the size of the source code files)? What advantages does this offer over interpreted languages for which such compilation is not necessary (and thus the program size is only the size of the code files)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现代解释语言通常会将代码编译为某种表示方式以加快执行速度……它可能不会写入磁盘,但肯定不能保证程序以更紧凑的形式表示。有些解释器会全力以赴地生成机器代码(例如Java JIT)。然后,解释器本身就位于内存中,该内存可能很大。
几点:
printf()
语句或类似内容,而对于输出格式化,C++ 有ostream
- 一种更复杂、可扩展的类型- 安全系统,具有(无论好坏)跨函数调用的持久状态、查询和设置该状态的例程、可定制缓冲的附加层、可定制字符类型和本地化,以及通常许多可以导致更小的内联函数或更大的程序取决于确切的用途和编译器设置。什么是最好的取决于您的应用程序和内存与性能目标。switch
并在 1 到 1000 之间随机分布 100 个 case 标签:一个编译器/语言可能决定“打包”100 个 case 并执行二进制操作搜索匹配项,另一个使用由 1000 个元素组成的稀疏数组并进行直接索引(这会浪费可执行文件中的空间,但通常会提高代码速度)。因此,很难根据可执行文件的大小得出结论。通常,随着程序变得更大、更复杂,内存使用和执行速度变得越来越重要。您不会看到用解释语言编写的操作系统、企业 Web 服务器或全功能商业文字处理器等系统,因为它们不具有可扩展性。
Modern interpreted languages do typically compile the code to some manner of representation for faster execution... it might not get written out to disk, but there's certainly no guarantee that the program is represented in a more compact form. Some interpreters go the whole hog and generate machine code anyway (e.g. Java JIT). Then there's the interpreter itself sitting in memory which can be large.
A few points:
printf()
statement or something similar, while for output formatting C++ hasostream
- a more complex, extensible and type-safe system with (for better or worse) persistent state across function calls, routines to query and set that state, an additional layer of customisable buffering, customisable character types and localisation, and generally a lot of small inline functions that can lead to smaller or larger programs depending on the exact use and compiler settings. What's best depends on your application and memory vs performance goals.switch
on an integer expression and have 100 case labels spread randomly between 1 and 1000: one compiler/languages might decide to "pack" the 100 cases and do a binary search for a match, another to use a sparsely populated array of 1000 elements and do direct indexing (which wastes space in the executable but typically makes for faster code). So, it's hard to draw conclusions based on executable size.Typically, memory usage and execution speed become increasingly important as the program gets larger and more complex. You don't see systems like Operating Systems, enterprise web servers or full-featured commercial word processors written in interpreted languages because they don't have the scalability.
解释型语言假设解释器可用,而编译的程序在大多数情况下是独立的。
Interpreted languages assume an interpreter is available while compiled programs are in most cases standalone.
举一个简单的例子:假设你有一个单行程序,
“打印”的作用是什么?当然很明显你要求其他代码来做一些工作吗?而且该代码不是免费的,需要运行的代码总数远远超过您编写的代码行数。在更现实的程序中,您可以利用许多复杂的库来管理窗口和其他 UI 功能、网络、数据库等。现在,无论该代码是捆绑到您的应用程序中还是从 DLL 加载,或者存在于解释器中,它都必须位于某个位置。
编译和解释以及中间解决方案(例如 Java 的编译/字节码解释方法)之间存在大量权衡。例如,您可能会考虑
Take a trivial case: Suppose you have a one line program
what does that "print" do? Surely it's clear that your asking some other code to do some work? And that code isn't free, the sum total of what needs to run is much more than the lines of code you write. In more realistic programs you exploit many sophisticated libraries managing windows and other UI features, networks, databases and so on. Now whether that code is bundled into your application or loaded from DLLs or is present in the interpreter it's got to be somewhere.
There are plenty of trades-off between compilation and interpretation, and intermediate solutions such as Java's compilation/byte-code interpreatation approach. For example, you might consider
通常,程序是用高级语言编写的,为了让这些程序由CPU执行,必须将程序转换为机器代码。此转换由编译器或解释器完成。
编译器仅进行一次转换,而解释器通常在每次执行程序时进行转换。
解释型程序的运行速度比编译型程序慢得多因为解释器每次执行时都必须分析程序中的每一条语句,然后执行所需的操作,而编译型代码只是执行由编译确定的固定上下文中的操作(这是存在大型二进制文件的原因)。
解释器的另一个缺点是它们必须作为附加软件存在于环境中才能运行源代码。
Usually, programs are written in higher level languages, for these programs to be executed by the CPU, the programs have to be converted to machine code. This conversion is done by a Compiler or an Interpreter.
A Compiler makes the conversion just once, while an Interpreter typically converts it every time a program is executed.
Interpreted programs run much slower than compiled programs because the interpreter must analyze each statement in the program each time it is executed and then perform the desired action, whereas the compiled code just performs the action within a fixed context determined by the compilation(which is the reason for presence of large sized binary files).
Another disadvantage of Interpreters is that they must be present in the enviornment as additional software to run the source code.