Home  >  Article  >  Backend Development  >  Is go language compilation fast?

Is go language compilation fast?

青灯夜游
青灯夜游Original
2021-06-07 14:00:083149browse

Go language compiles quickly because of: 1. Use of the import reference management method; 2. No template compilation burden; 3. Bootstrap compiler optimization after version 1.5; 4. Fewer keys There are a total of 25 keywords in the Go language, which can simplify code analysis during the compilation process.

Is go language compilation fast?

The operating environment of this tutorial: Windows 10 system, GO 1.11.2, Dell G3 computer.

The main features of Go now are compilation speed, execution speed, memory management and concurrent programming.

Why is Go’s compilation fast?

Of course, designing the Go language does not start completely from scratch. Initially, the Go team tried to design and implement a compilation front-end for the Go language. The C-based gcc compiler is compiled into machine code. This front-end compiler for gcc is gccgo, one of the current Go compilers.

Rather than talking about why Go compiles fast, let’s first talk about why C compiles slowly. C can also be compiled with gcc. Most of the difference in compilation speed is likely to come from the language design itself.

Before discussing the problem, one thing that needs to be explained is: the compilation speeds compared here are all under static compilation.

The difference between static compilation and dynamic compilation

Static compilation: When the compiler compiles the executable file, it must extract the link library used. The link is packaged into an executable file, and the compilation result is only one executable file.
Dynamic compilation: The executable file needs to be accompanied by an independent library file. Do not package the library into the executable file to reduce the size of the executable file. Just call the library during execution.

The two methods have their own advantages and disadvantages. The former does not need to manage the compatibility issues of different versions of libraries, while the latter can reduce memory and storage usage (because different programs can share the same Library). Which of the two methods is better or weaker depends on the specific engineering issues. Go's default compilation method is static compilation.

Back to the question we want to discuss: Why is C compilation slow?

There are two main reasons why C compilation is slow:

1. The include method of header files

2. The compilation of templates

C Using the include method to reference a header file will increase the code that needs to be compiled at a multiplier level. For example, when the same header file is included by N files under the same project, the compiler will introduce the header file. into every piece of code, so the same header file will be compiled N times (this is unnecessary most of the time);

The template used by C is to support generic programming. When writing different Type-generic functions can provide great convenience, but this will add a lot of unnecessary compilation burden to the compiler.

Of course, C has many subsequent optimization methods for these two problems, but for many developers, they do not want to spend too much time and energy on this.

Most of the later programming languages ​​used import module instead of include
header file in the way of introducing files. Import solves the problem of repeated compilation. Of course, Go also uses it. import method; on the issue of template compilation, since Go follows a simple approach in its design philosophy, it does not incorporate functional programming into the design framework, so there is naturally no time overhead caused by template compilation (which is also a lot without generic support). Reasons why people are dissatisfied with the Go language).

So in my opinion, Go compiles quickly mainly for four reasons:

1. The import reference management method is used;

2. No template compilation burden;

3. Bootstrap compiler optimization after version 1.5;

4. Fewer keywords.

There are 25 keywords in Go language:

break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var

The reason why the keywords in the Go language are deliberately kept so few is to simplify code analysis during the compilation process. Like other languages, keywords cannot be used as identifiers.

So in order to speed up compilation and give up C and switch to Go, you should also consider whether to give up the advantages of generic programming.

Note: Generics may be supported in Go 2 version.

note:
Although Go cannot yet achieve the ultimate performance of C, it is very close in most cases;
The time overhead of the algorithm between Go and Java It's hard to tell the difference, but in terms of memory overhead, Java is much higher;
In most of the above cases, Go is much better than Python in at least time and memory overhead;

Recommended learning: Golang tutorial

The above is the detailed content of Is go language compilation fast?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn