2017-12-08 12:18
浏览 2.2k

为什么用CGO_ENABLED = 0编译慢?

When writing programs which utilize network, you can see quite noticeable slowdown of compilation with CGO_ENABLED=0.

For example, the simplest HTTP server:

package main

import (

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hi! glad you requested %s.
", r.URL.Path[1:])

func main() {
    port := flag.Int("port", 9000, "")

    http.HandleFunc("/", handler)
    err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil)
    if err != nil {

the timings are:

% time go build
go build  0.46s user 0.06s system 131% cpu 0.396 total
% time CGO_ENABLED=0 go build
CGO_ENABLED=0 go build  3.93s user 0.15s system 143% cpu 2.849 total

So far I'm not using bindings to C so CGo handling or not seems irrelevant, what I'd like to do is to compile 100% static binary, but not if there's such slowdown.

What is the cause of such behavior?

图片转代码服务由CSDN问答提供 功能建议

在编写利用网络的程序时,您会看到 CGO_ENABLED = 0的编译速度明显降低


  “ fmt” 
“ log” 
“ net / http” 
func处理程序(带有http.ResponseWriter,* * http.Request){
 fmt.Fprintf(w,“嗨!很高兴您提出请求 %s。
“,r.URL.Path [1:])
func main(){
 port:= flag.Int(” port“,9000,”“)
标志。  Parse()
 http.HandleFunc(“ /”,handler)
 err:= http.ListenAndServe(fmt.Sprintf(“:%d”,* port),nil)
 if err!= nil {  


 % 时间进行构建
go构建0.46s用户0.06s系统131%cpu 0.396总计
%时间CGO_ENABLED = 0进行构建
CGO_ENABLED = 0进行构建3.93s用户0.15s系统143%cpu 2.849总计
  <  / pre> 

到目前为止,我没有使用对C的绑定,所以似乎CGo处理还是不 无关紧要的是,我想编译100%静态二进制文件,但是如果出现这种情况,则不会。

这种行为的原因是什么? < / DIV>

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongzhang8680 2017-12-08 14:06

    The problem is that the standard library packages are built without flags. CGO_ENABLED changes build flags and therefore it can't use the pre-built packages so most of the standard library needs to be rebuilt.

    As the other answer mentioned, go build -i will install the packages built with the new flags, but that won't really solve much because if you install packages built with CGO_ENABLED=0, it will speed up all future builds with CGO_ENABLED=0, but it will slow down all the builds without it.

    Unfortunately the way pre-built packages are installed by default today is pretty inefficient because everything goes into the same directory under the same names regardless of how it's built. If you want to be able to have fast builds of go programs with different flags, besides doing go build -i you also need to use the the -installsuffix and/or the -pkgdir flags. In the system I work in, we have a handful of different compilation modes, each mode has different flags (because of a lot of old C code we interface with) and each mode also has its own -pkgdir.

    打赏 评论
  • dtd58256 2017-12-08 12:28

    This is the time spent re-building dependencies. Go build by default does not save rebuilt dependencies. See the -i flag: The -i flag installs the packages that are dependencies of the target.

    Let's try your program with -i instead:

    $ time go build -i . 
    real    0m0.337s
    user    0m0.343s
    sys 0m0.121s
    $ time CGO_ENABLED=0 go build -i .    
    real    0m2.135s
    user    0m3.098s
    sys 0m0.196s
    $ time CGO_ENABLED=0 go build .
    real    0m0.329s
    user    0m0.367s
    sys 0m0.085s
    $ time go build .    
    real    0m2.588s
    user    0m3.393s
    sys 0m0.300s

    The first time you switch cgo mode, it needs to rebuild dependencies. If you specify -i, it will save them and the second build call will be much faster.

    打赏 评论

相关推荐 更多相似问题