简体   繁体   English

go 命令在我的 Windows 机器上运行缓慢,在我的 linux 上运行良好

[英]go commands run slow on my windows machine, and fine on my linux

I don't know if someone else is running into this problem.我不知道其他人是否遇到了这个问题。 I have this main.go file:我有这个 main.go 文件:

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}

when I run go build, it takes 5 secs to run it (regardless if it is the first time I run it or if it is the second time)当我运行 go build 时,运行它需要 5 秒(无论是第一次运行还是第二次运行)

PS> Measure-Command {Start-Process go build -wait}

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 5
Milliseconds      : 151
Ticks             : 51514117
TotalDays         : 5.96228206018519E-05
TotalHours        : 0.00143094769444444
TotalMinutes      : 0.0858568616666667
TotalSeconds      : 5.1514117
TotalMilliseconds : 5151.4117

But when I run it on a linux machine:但是当我在 linux 机器上运行它时:

time go build

real    0m2.017s
user    0m0.054s
sys     0m1.915s

and when I run it for the second time:当我第二次运行它时:

time go build

real    0m0.120s
user    0m0.072s
sys     0m0.088s

This is not only build, but also some of go tools such as fmt.这不仅是build,还有fmt等一些go工具。 It takes 0.12 seconds on linux, but almost 3 seconds on windows.在 linux 上需要 0.12 秒,但在 windows 上几乎需要 3 秒。 Other tools like guru, gocode, etc. suffer the same problem, making code development very slow.其他工具如 guru、gocode 等也遇到同样的问题,使得代码开发非常缓慢。

I'm using golang 1.11.我正在使用 golang 1.11。 I'm using an SSD and everything is running locally.我正在使用 SSD,一切都在本地运行。 Sorry I wish I could be more helpful but I really have no idea where to start to debug this.抱歉,我希望我能提供更多帮助,但我真的不知道从哪里开始调试。

Does anyone has an idea what's going on?有谁知道发生了什么?

It seems that the build cache is disabled on your Windows and enabled on your Linux. 似乎在Windows上禁用了构建缓存 ,在Linux上启用了该缓存

Go build keeps the result of compilations and reuse it if the .go file hasn't changed. Go build保留编译结果,如果.go文件未更改,则重新使用它。 That's why your second build is so fast in Linux. 这就是为什么您的第二个构建在Linux中如此之快的原因。

If you disable it, not only your code, but also all the dependancies must be recompiled each time. 如果禁用它,则不仅必须重新编译代码,还必须重新编译所有依赖关系。 Thus even if you change your code, all the libs (here "fmt") are already in cache. 因此,即使您更改代码,所有库(此处为“ fmt”)也已经在缓存中。

To test it, run go clean -cache before the go build on Linux, and see if the time correspond to the time on your Windows. 要对其进行测试, go clean -cache在Linux上的go build之前运行go clean -cache ,然后查看时间是否与Windows上的时间相对应。 Then if it matches, you have to find why the build cache is disabled on Windows. 然后,如果匹配,则必须找出为什么在Windows上禁用了构建缓存。

You can see the cache directory by typing go env GOCACHE . 您可以通过输入go env GOCACHE来查看缓存目录。 If the response is off, the cache is off. 如果响应关闭,则缓存关闭。 Otherwise verify that the repository exists and it has the right permissions. 否则,请验证存储库是否存在并且具有正确的权限。

You can choos the cache directory by setting the value of the GOCACHE environment variable (sorry I don't know how to do this in Windows). 您可以通过设置GOCACHE环境变量的值来选择缓存目录(抱歉,我不知道如何在Windows中执行此操作)。

I was having same issue.我有同样的问题。 Turns out I had a really big directory with 1000s of large files in the same location where the binary was.原来我有一个非常大的目录,在二进制文件所在的同一位置有 1000 个大文件。 Moving the big dir out to a different location resolved the issue.将大目录移到其他位置解决了该问题。 This was in a macOS.这是在 macOS 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的程序在Windows机器上崩溃,但在Linux上运行正常 - My program crashes on the Windows machine yet it works fine on the Linux 为什么我的游戏可以在Linux上正常运行,而不能在Windows上运行? (Java .jar) - Why does my game run fine on Linux, but not in Windows? (Java .jar) 为什么git在Windows计算机上运行缓慢? - Why is git slow on my windows machine? 我有几个Linux命令,我想在Windows机器上运行。 如何在Windows上从Python运行Linux命令 - I have a couple of Linux commands, which i want to run on windows machine. How to Run Linux commands from Python on Windows 为什么我的文件/目录操作在 Windows 上可以正常工作,但在 Linux 上却不能? - Why does my file/dir manipulation works fine on Windows but not on Linux? 为什么我的Windows命令行允许Linux命令? - Why does my Windows command line allow Linux commands? 如何在 windows 中运行“source ~/.my_commands.sh”? - How to run 'source ~/.my_commands.sh' in windows? 如何在 Windows 上运行 linux 终端命令? - How to run linux terminal commands on windows? pyodbc在我的机器上非常慢,但在其他机器上却没有 - pyodbc is very slow on my machine but not on other machines 我的python程序在Windows 7中可以正常工作并打印到控制台,但在Linux Mint中却报错? - My python program works fine in Windows 7 and prints to the console but gives an error in Linux Mint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM