我正在尝试解决代码谜题的出现(所以对于那些还没有完成第一天的人来说剧透警告),我遇到了一些我无法理解的事情。
我有一个函数,它根据其他一些数字列表生成一个数字列表,并返回第二次遇到的第一个数字:
func findFirstDoubleFrequency(freqs []int) int {
seen := map[int]bool{0: true}
freq := 0
for {
for _, f := range freqs {
freq += f
if seen[freq] == true {
return freq
}
seen[freq] = true
}
}
}
当我使用 go run
运行我的代码时,函数需要约 15 毫秒才能完成。但是,当我使用 go build
构建可执行文件并运行它时,需要大约 40 毫秒才能完成。我真的很想知道为什么这些运行之间的执行时间有如此大的差异。他们不应该是一样的吗?或者是像 GC 这样的东西用 go build
可执行文件减慢了速度?
最佳答案
Stack Overflow
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
您的基准无效。这是不完整的。它不可重现。
在 Go 中,使用 testing
包对代码进行基准测试。例如,
package main
import (
"math/rand"
"testing"
)
func findFirstDoubleFrequency(freqs []int) int {
seen := map[int]bool{0: true}
freq := 0
for {
for _, f := range freqs {
freq += f
if seen[freq] == true {
return freq
}
seen[freq] = true
}
}
}
func BenchmarkFirstFrequency(b *testing.B) {
freqs := make([]int, 1000)
for i := range freqs {
freqs[i] = rand.Intn(len(freqs)/10)
}
b.ReportAllocs()
b.ResetTimer()
for N := 0; N < b.N; N++ {
findFirstDoubleFrequency(freqs)
}
}
输出:
$ go test t94_test.go -bench=.
goos: linux
goarch: amd64
BenchmarkFirstFrequency-4 1000000 7206 ns/op 3342 B/op 16 allocs/op
$
警告:你有一个可能的无限循环:
package main
import (
"math/rand"
"testing"
)
func findFirstDoubleFrequency(freqs []int) int {
seen := map[int]bool{0: true}
freq := 0
for {
for _, f := range freqs {
freq += f
if seen[freq] == true {
return freq
}
seen[freq] = true
}
}
}
func BenchmarkFirstFrequency(b *testing.B) {
freqs := make([]int, 1000)
for i := range freqs {
freqs[i] = rand.Intn(len(freqs))
}
b.ReportAllocs()
b.ResetTimer()
for N := 0; N < b.N; N++ {
findFirstDoubleFrequency(freqs)
}
}
输出:
$ go test t94_test.go -bench=.
goos: linux
goarch: amd64
BenchmarkFirstFrequency-4 fatal error: runtime: out of memory
关于go - 编译二进制比运行慢 "go run",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580523/