go - 编译二进制比运行慢 "go run"

标签 go

我正在尝试解决代码谜题的出现(所以对于那些还没有完成第一天的人来说剧透警告),我遇到了一些我无法理解的事情。

我有一个函数,它根据其他一些数字列表生成一个数字列表,并返回第二次遇到的第一个数字:

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/

相关文章:

json - 为什么 golang json number 不能像 "10"那样转换 int 或 string int?

go - 使用 Go 依赖项的修改版本的问题

Go - 从重定向的 .txt 文件获取输入时,Scanf 和 ReadString 与换行符发生冲突

Govendor 作为类型错误

pointers - 声明一个指向结构的指针

dictionary - 删除 map 中的键

json - 在 Go 中,为什么 JSON null 有时不会传递给 UnmarshalJSON 进行解码?

go - 同时计算树叶

c - 如何使用 GO 导入用 C 编写的 DLL 函数?

go - 如何将请求主体解码/解码为 Golang 结构和结构 slice ?