登录
首页 >  Golang >  Go问答

为何生成器执行速度低于回调函数?

来源:stackoverflow

时间:2024-03-27 13:00:33 286浏览 收藏

在 Go 语言中,使用生成器和回调函数迭代一系列值时,性能表现出显著差异。虽然二者均调用匿名函数相同次数,但生成器的执行速度明显低于回调函数,这一现象引起了困惑。经调查发现,性能差距与基准代码有关,使用匿名函数进行的延迟操作显著降低了生成器的性能。改进后的代码通过直接递增局部变量的方式取代延迟操作,获得了与回调函数更为接近的性能结果。

问题内容

我试图找出 go 中迭代一系列值最快的模式。每个都有明显的优点和缺点,但速度将是我的用例的一个重要因素

毫不奇怪,渠道是最慢的。

但是我很惊讶看到回调和生成器模式之间存在巨大差异,我试图了解发生了什么。在我看来,性能应该差不多,但事实并非如此。

两者都调用匿名函数的次数相同,但我想知道是否有与上下文切换相关的东西。

下层发生了什么?

我得到这些结果:

benchmarkiteratormethods/generator-8               31970             36196 ns/op
benchmarkiteratormethods/callback-8              1000000              1193 ns/op
benchmarkiteratormethods/channel-8                  7999            148906 ns/op

我的基准代码:

package test

import (
    "testing"
)

const iteratorTestIterations = 1000

func iteratorGeneratorFunctionForTests() func() (int, bool) {
    i := 0
    return func() (int, bool) {
        for i < iteratorTestIterations {
            defer (func() {
                i++
            })()
            return i, false
        }

        return 0, true
    }
}
func iteratorCallbackForTests(callback func(value int) bool) {
    for i := 0; i < iteratorTestIterations; i++ {
        shouldContinue := callback(i)
        if !shouldContinue {
            break
        }
    }
}
func iteratorChannelForTests() chan int {
    channel := make(chan int)
    go (func() {
        for i := 0; i < iteratorTestIterations; i++ {
            channel <- i
        }

        close(channel)
    })()

    return channel
}

func BenchmarkIteratorMethods(b *testing.B) {
    b.Run("generator", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            iterator := iteratorGeneratorFunctionForTests()
            for {
                value, end := iterator()
                if end {
                    break
                }
                _ = value
            }
        }
    })
    b.Run("callback", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            iteratorCallbackForTests(func(value int) bool {
                _ = value
                return true
            })
        }
    })
    b.Run("channel", func(b *testing.B) {
        for i := 0; i < b.N; i++ {
            for value := range iteratorChannelForTests() {
                _ = value
            }
        }
    })
}

解决方案


我找到了原因。这实际上与我的代码有关。使用函数延迟太慢。

我用这个替换了第一个函数:

func iteratorgeneratorfunctionfortests() func() (int, bool) {
    i := 0
    return func() (int, bool) {
        for i < iteratortestiterations {
            value := i
            i++
            return value, false
        }

        return 0, true
    }
}

现在我得到了更合乎逻辑的结果:

BenchmarkIteratorMethods/generator-8              773698              1442 ns/op
BenchmarkIteratorMethods/callback-8               966046              1168 ns/op
BenchmarkIteratorMethods/channel-8                  7710            145243 ns/op

理论要掌握,实操不能落!以上关于《为何生成器执行速度低于回调函数?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>