为什么构建Go包需要这么多的CPU资源?

huangapple go评论100阅读模式
英文:

Why Go takes so much CPU to build a package?

问题

我下载了一个来自GitHub的Go语言包。它的大小适中。当我从源代码编译它时,我的电脑变得很慢,因为我有多个Go语言编译进程,并且它们占用了大量的CPU资源。

Go语言是如何实现并发编译的呢?在编译时有没有可以调整使用的CPU数量的参数?

英文:

I have download a golang package from github. it is in middle size. when compiling it from source, my computor gets slow down for i have more than one golang compile process and it takes much of the cpu.

how does golang make it to do concurrent compile?
any params to turn the number of cpu it use when compile?

答案1

得分: 3

Go语言使用大量的CPU,因为它试图尽可能快地构建,就像其他编译器一样。这也可能是因为你正在使用一个使用cgo的包,这会大大增加编译时间,因为编译中等到大型C库通常是相当密集的。

你可以通过设置GOMAXPROCS环境变量来控制Go使用的进程数。例如,GOMAXPROCS=1 go get ...将限制Go只使用1个进程(因此只使用1个CPU核心)。然而,这不会影响cgo可能调用的外部编译器使用的进程数。

如果你需要进一步控制CPU,在基于Unix的系统上,你可以使用nice命令来改变进程的优先级,使其他程序具有更高的CPU优先级,从而减少计算机的卡顿。

英文:

Go is using a lot of CPU because it's trying to build as fast as possible, like any other compiler. It may also be because you're using a package that is using cgo, which can drastically increase compiling times as compiling medium to large C libraries is often quite intensive.

You can control the number of processes Go is using by setting the GOMAXPROCS environment variable. Such as GOMAXPROCS=1 go get ... to limit Go to only use 1 process (and thus only 1 CPU core). This however does not affect the number of processes used by external compilers that cgo may invoke.

If you need further CPU control, on Unix based systems you can use the nice command to change the priority of processes such that other programs have higher CPU priority, making your computer less sluggish.

huangapple
  • 本文由 发表于 2016年8月21日 21:48:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/39064914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定