dongxinxin7809 2014-02-10 18:02
浏览 33
已采纳

或突然变慢,取决于阵列大小

I wrote a simple program that ORs all the values contained in a huge go slice. When I use a 10 times bigger slice, I would expect a 10x performance drop. However when executing the provided test, there is a huge performance gap. The program output is the following :

oadam@oadam-Latitude-E6510:~/$ go test -bench .
testing: warning: no tests to run
PASS
BenchmarkLittle 2000000000           0.11 ns/op
BenchmarkBig           1    2417869962 ns/op
ok      _/home/oadam/   5.048s

And the code

package main

import (
    "math/rand"
    "testing"
)

const (
    little = 5000000
    big    = 50000000
)

var a = make([]uint32, big)

func benchOR(b *testing.B, l int) {
    for i := 0; i < l; i++ {
        a[i] = rand.Uint32()
    }

    var result uint32
    for i := 0; i < l; i++ {
        result |= a[i]
    }
}

func BenchmarkLittle(b *testing.B) {
    benchOR(b, little)
}

func BenchmarkBig(b *testing.B) {
    benchOR(b, big)
}

EDIT: must be a bug in go test -bench. Using a manual timing I don't reproduce

package main

import (
    "log"
    "math/rand"
    "time"
)

const (
    little = 5000000
    big    = 50000000
)

var a = make([]uint32, big)

func initA(l int) {
    for i := 0; i < l; i++ {
        a[i] = rand.Uint32()
    }
}

func test(l int) uint32 {
    var result uint32
    for i := 0; i < l; i++ {
        result |= a[i]
    }
    return result
}

func main() {
    initA(little)
    var before = time.Now()
    test(little)
    log.Println(time.Since(before))

    initA(big)
    var before2 = time.Now()
    test(big)
    log.Println(time.Since(before2))

}
  • 写回答

2条回答 默认 最新

  • douyue2313 2014-02-10 20:11
    关注

    The problem is that you are not using b.N, which tells you how many times to run the benchmark. Also, if you only want to benchmark the ORing, you should probably just initialize the array once, or at least call b.ResetTimer() so the initialization is not counted.

    Here's what I ended up with, which gives the expected results:

    package main
    
    import (
        "math/rand"
        "testing"
    )
    
    const (
        little = 5000000
        big    = 50000000
    )
    
    var a = make([]uint32, big)
    
    func init() {
        for i := 0; i < big; i++ {
            a[i] = rand.Uint32()
        }
    }
    
    func benchOR(b *testing.B, l int) {
        var result uint32
        for _, u := range a[:l] {
            result |= u
        }
    }
    
    func BenchmarkLittle(b *testing.B) {
        for i := 0; i < b.N; i++ {
            benchOR(b, little)
        }
    }
    
    func BenchmarkBig(b *testing.B) {
        for i := 0; i < b.N; i++ {
            benchOR(b, big)
        }
    }
    

    My results:

    BenchmarkLittle      500       3222064 ns/op
    BenchmarkBig          50      32268023 ns/op
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?