dousi8931 2019-04-07 14:34
浏览 75

为什么去基准测试在不同的地方用相同的代码显示不同的结果?

I benchmarked golang system package "math/bits". It is fast. I benchmarked the same codes copied from "math/bits", it is about 3 times slower.

I wonder what is the differences between user's code and the system code when compiling, linking or benchmarking?

// x_test.go
package x_test

import (
    "math/bits"
    "testing"
)

// copied from "math/bits"
const DeBruijn64 = 0x03f79d71b4ca8b09

var Input uint64 = DeBruijn64
var Output int

const m0 = 0x5555555555555555 // 01010101 ...
const m1 = 0x3333333333333333 // 00110011 ...
const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
const m3 = 0x00ff00ff00ff00ff // etc.
const m4 = 0x0000ffff0000ffff

func OnesCount64(x uint64) int {
    const m = 1<<64 - 1
    x = x>>1&(m0&m) + x&(m0&m)
    x = x>>2&(m1&m) + x&(m1&m)
    x = (x>>4 + x) & (m2 & m)
    x += x >> 8
    x += x >> 16
    x += x >> 32
    return int(x) & (1<<7 - 1)
}

// copied from "math/bits" END


func BenchmarkMine(b *testing.B) {
    var s int
    for i := 0; i < b.N; i++ {
        s += OnesCount64(uint64(i))
    }
    Output = s
}

func BenchmarkGo(b *testing.B) {
    var s int
    for i := 0; i < b.N; i++ {
        s += bits.OnesCount64(uint64(i))
    }
    Output = s
}

And running it shows the different result:

go test x_test.go  -bench=.
goos: darwin
goarch: amd64
BenchmarkMine-4         500000000                3.32 ns/op
BenchmarkGo-4           2000000000               0.96 ns/op

The two benchmarks should result in similar results. But not.

  • 写回答

1条回答 默认 最新

  • dtqf81594 2019-04-07 17:09
    关注

    After digging into go source code I found that during compiling go replaces math/bits:OnesCount64 with an instruction implementation: go/src/cmd/compile/internal/gc/ssa.go:3428 :makeOnesCountAMD64.

    When calling math/bits.OnesCount64 it actually does use the codes in math/bits.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题