Giter VIP home page Giter VIP logo

Comments (12)

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

性能测试的代码

// Exported (global) variable serving as input for some
// of the benchmarks to ensure side-effect free calls
// are not optimized away.
var Input uint64 = 285870213051353865

// Exported (global) variable to store function results
// during benchmarking to ensure side-effect free calls
// are not optimized away.
var Output int

func BenchmarkLeadingZeros(b *testing.B) {
   var s int
   for i := 0; i < b.N; i++ {
      s += bsr2(uint(Input) >> (uint(i) % bits.UintSize))
   }
   Output = s
}

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

用于对比的代码

func bsr(x uint) int {
	r := 0
	for x != 0 {
		x = x >> 1
		r += 1
	}
	return r - 1
}

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

我写的代码


func bsr2(x uint) int {
	if bits.UintSize == 32 {
		return Len32(uint32(x))
	}
	return Len64(uint64(x))
}

// Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len32(x uint32) (n int) {
	if x >= 1<<16 {
		x >>= 16
		n = 16
	}
	if x >= 1<<8 {
		x >>= 8
		n += 8
	}
	return n + int(bsr8tab[x])
}

// Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len64(x uint64) (n int) {
	if x >= 1<<32 {
		x >>= 32
		n = 32
	}
	if x >= 1<<16 {
		x >>= 16
		n += 16
	}
	if x >= 1<<8 {
		x >>= 8
		n += 8
	}
	return n + int(bsr8tab[x])
}

var bsr8tab = [256]uint8{
	0x00, 0x00, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
	0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
	0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
	0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
	0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
}

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

性能测试结果对比

goos: darwin
goarch: amd64
pkg: code.byted.org/middleware/mcache
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkLeadingZeros
BenchmarkLeadingZeros-12    	610674840	         1.927 ns/op
goos: darwin
goarch: amd64
pkg: code.byted.org/middleware/mcache
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkLeadingZeros
BenchmarkLeadingZeros-12    	98333178	        12.16 ns/op

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

证明结果无误的例子

func TestBsr(t *testing.T) {
	assert.Equal(t, bsr(4), bsr2(4), 2)
	assert.Equal(t, bsr(24), bsr2(24), 4)
	assert.Equal(t, bsr((1<<10)-1), bsr2((1<<10)-1), 9)
	assert.Equal(t, bsr((1<<30)+(1<<19)+(1<<16)+(1<<1)), bsr2((1<<30)+(1<<19)+(1<<16)+(1<<1)), 30)
}

/usr/local/go/bin/go tool test2json -t /private/var/folders/41/3lxydnv150369kfnhqjbr2j40000gn/T/___TestBsr_in_code_byted_org_middleware_mcache.test -test.v -test.paniconexit0 -test.run ^\QTestBsr\E$
=== RUN TestBsr
--- PASS: TestBsr (0.00s)
PASS

from gopkg.

PureWhiteWu avatar PureWhiteWu commented on June 20, 2024

要不提个 pr,顺便,Len32 和 Len64 我觉得可以不用暴露出去,改为小写?

from gopkg.

jxskiss avatar jxskiss commented on June 20, 2024

Len32 和 Len64 合并成单个函数是不是就可以呀,
if bits.UintSize == 32 是常量比较,我理解编译器可以优化掉,所以单个函数也不会增加额外的计算成本吧。

from gopkg.

jxskiss avatar jxskiss commented on June 20, 2024

个人看法,bits.Len32 和 bits.Len64 这些标准库的函数没必要复制一份?
bits.Len64(x) - 1 跟上面的代码是等价的吧。

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

个人看法,bits.Len32 和 bits.Len64 这些标准库的函数没必要复制一份?
bits.Len64(x) - 1 跟上面的代码是等价的吧。

是的。
我是写完之后才意识到这一点。

from gopkg.

luchsh avatar luchsh commented on June 20, 2024

并且针对64位操作系统和32位操作系统有不同的优化。

能贴一下你发现的是哪个库会用到吗?

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

from gopkg.

zdyj3170101136 avatar zdyj3170101136 commented on June 20, 2024

from gopkg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.