我在谷歌上搜索了一下Go的网站,但我找不到Go非凡的构建时间的解释。它们是语言特性(或缺乏特性)的产物、高度优化的编译器还是其他什么?我并不是在推广围棋;我只是好奇。
当前回答
虽然上面的大部分都是正确的,但有一点非常重要,没有被真正提到:依赖管理。
Go只需要包含您直接导入的包(就像那些已经导入的包一样)。这与C/ c++形成鲜明对比,在C/ c++中,每个文件都包含x头文件,其中包含y头文件等等。总结:Go的编译需要线性时间w.r.t到导入包的数量,而C/ c++需要指数级的时间。
其他回答
虽然上面的大部分都是正确的,但有一点非常重要,没有被真正提到:依赖管理。
Go只需要包含您直接导入的包(就像那些已经导入的包一样)。这与C/ c++形成鲜明对比,在C/ c++中,每个文件都包含x头文件,其中包含y头文件等等。总结:Go的编译需要线性时间w.r.t到导入包的数量,而C/ c++需要指数级的时间。
Go编译器比大多数C/ c++编译器快得多的原因有很多:
Top reason: Most C/C++ compilers exhibit exceptionally bad designs (from compilation speed perspective). Also, from compilation speed perspective, some parts of the C/C++ ecosystem (such as editors in which programmers are writing their code) aren't designed with speed-of-compilation in mind. Top reason: Fast compilation speed was a conscious choice in the Go compiler and also in the Go language The Go compiler has a simpler optimizer than C/C++ compilers Unlike C++, Go has no templates and no inline functions. This means that Go doesn't need to perform any template or function instantiation. The Go compiler generates low-level assembly code sooner and the optimizer works on the assembly code, while in a typical C/C++ compiler the optimization passes work on an internal representation of the original source code. The extra overhead in the C/C++ compiler comes from the fact that the internal representation needs to be generated. Final linking (5l/6l/8l) of a Go program can be slower than linking a C/C++ program, because the Go compiler is going through all of the used assembly code and maybe it is also doing other extra actions that C/C++ linkers aren't doing Some C/C++ compilers (GCC) generate instructions in text form (to be passed to the assembler), while the Go compiler generates instructions in binary form. Extra work (but not much) needs to be done in order to transform the text into binary. The Go compiler targets only a small number of CPU architectures, while the GCC compiler targets a large number of CPUs Compilers which were designed with the goal of high compilation speed, such as Jikes, are fast. On a 2GHz CPU, Jikes can compile 20000+ lines of Java code per second (and the incremental mode of compilation is even more efficient).
Go对所有文件导入一次依赖项,因此导入时间不会随着项目大小呈指数增长。 更简单的语言学意味着解释它们需要更少的计算。
还有什么?
依赖性分析。
Go FAQ通常包含以下句子:
围棋为软件提供了一个模型 产生依赖性的构造 分析简单,避免了很多 c风格include文件的开销和 库。
虽然这个短语不再出现在FAQ中,但这个主题在谷歌的Go谈话中进行了详细阐述,该谈话比较了C/ c++和Go的依赖分析方法。
这是快速编译的主要原因。这是设计出来的。
摘自Alan Donovan和Brian Kernighan所著的《the Go Programming Language》一书:
Go compilation is notably faster than most other compiled languages, even when building from scratch. There are three main reasons for the compiler’s speed. First, all imports must be explicitly listed at the beginning of each source file, so the compiler does not have to read and process an entire file to determine its dependencies. Second, the dependencies of a package form a directed acyclic graph, and because there are no cycles, packages can be compiled separately and perhaps in parallel. Finally, the object file for a compiled Go package records export information not just for the package itself, but for its dependencies too. When compiling a package, the compiler must read one object file for each import but need not look beyond these files.
推荐文章
- .NET反射的成本有多高?
- Xcode:构建失败,但没有错误消息
- 在c#中检查字符串是否只包含数字的最快方法
- JavaScript变量声明在循环外还是循环内?
- 用自己的语言编写编译器
- 为什么C语言这么快,为什么其他语言没有这么快或更快?
- String与StringBuilder
- 为什么引入无用的MOV指令会加速x86_64汇编中的紧循环?
- 如何在SQL中有效地计数列值的发生?
- 如何正确的种子随机数生成器
- 使用curl在PHP中获取HTTP代码
- 确定PHP中是否存在数组键的更快更好的方法是什么?
- 如何在O(n)中找到长度为n的无序数组中的第k大元素?
- postgresql COUNT(DISTINCT…)非常慢
- 按(任意)字段名对结构数组进行排序的最短方法是什么?