我在谷歌上搜索了一下Go的网站,但我找不到Go非凡的构建时间的解释。它们是语言特性(或缺乏特性)的产物、高度优化的编译器还是其他什么?我并不是在推广围棋;我只是好奇。
当前回答
围棋的设计就是要快,这一点已经体现出来了。
Dependency Management: no header file, you just need to look at the packages that are directly imported (no need to worry about what they import) thus you have linear dependencies. Grammar: the grammar of the language is simple, thus easily parsed. Although the number of features is reduced, thus the compiler code itself is tight (few paths). No overload allowed: you see a symbol, you know which method it refers to. It's trivially possible to compile Go in parallel because each package can be compiled independently.
请注意,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.
简单(用我自己的话说),因为语法非常容易(分析和解析)
例如,没有类型继承意味着不需要进行有问题的分析,以确定新类型是否遵循基类型施加的规则。
例如,在这个代码示例:"interfaces"中,编译器在分析该类型时不会去检查预期的类型是否实现了给定的接口。只有在它被使用之前(如果它被使用)才会执行检查。
另一个例子,编译器会告诉你你是否声明了一个变量而没有使用它(或者如果你应该持有一个返回值而你没有)
以下代码不能编译:
package main
func main() {
var a int
a = 0
}
notused.go:3: a declared and not used
这种强制和原则使生成的代码更安全,并且编译器不必执行程序员可以执行的额外验证。
总的来说,所有这些细节使语言更容易解析,从而导致快速编译。
再一次,用我自己的话来说。
我认为不是Go编译器快,而是其他编译器慢。
C和c++编译器必须解析大量的头文件——例如,编译c++“hello world”需要编译18k行代码,这几乎是半兆字节的源代码!
$ cpp hello.cpp | wc
18364 40513 433334
Java和c#编译器在VM中运行,这意味着在它们编译任何东西之前,操作系统必须加载整个VM,然后必须将它们从字节码编译为本机代码,所有这些都需要一些时间。
编译的速度取决于几个因素。
有些语言被设计为快速编译。例如,Pascal被设计为使用单遍编译器进行编译。
编译器本身也可以优化。例如,Turbo Pascal编译器是用手工优化的汇编器编写的,结合语言设计,可以在286类硬件上运行非常快的编译器。我认为即使是现在,现代的Pascal编译器(例如FreePascal)也比Go编译器快。
编译效率是主要的设计目标:
最后,它的目的是要快:在一台计算机上构建一个大型可执行文件最多只需要几秒钟。为了实现这些目标,需要解决一些语言问题:表达性强但轻量级的类型系统;并发性和垃圾回收;刚性依赖规范;等等。常见问题解答
关于与解析相关的特定语言特性,语言常见问题解答非常有趣:
第二,该语言被设计成易于分析,并且可以在没有符号表的情况下进行解析。
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).
推荐文章
- 在Mac OS X上安装/升级gradle
- 错误:执行失败的任务':app:compileDebugKotlin'。>编译错误。详细信息请参见日志
- 为什么更快地检查字典是否包含键,而不是捕捉异常,以防它不?
- 当启用c++ 11时,std::vector性能回归
- 使用c++ 11的“auto”可以提高性能吗?
- std::vector比普通数组慢很多吗?
- 如何获取切片的最后一个元素?
- 不能将数据(类型接口{})转换为类型字符串:需要类型断言
- 优化PostgreSQL进行快速测试
- 如果性能很重要,我应该使用Java的String.format()吗?
- 使用“var”会影响性能吗?
- 在Python中哪个更快:x**。5还是math.sqrt(x)?
- 解析Go中的RFC-3339 / ISO-8601日期-时间字符串
- 声明一个常量数组
- 函数可以作为参数传递吗?