Comments (12)
性能测试的代码
// 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.
用于对比的代码
func bsr(x uint) int {
r := 0
for x != 0 {
x = x >> 1
r += 1
}
return r - 1
}
from gopkg.
我写的代码
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.
性能测试结果对比
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.
证明结果无误的例子
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.
要不提个 pr,顺便,Len32 和 Len64 我觉得可以不用暴露出去,改为小写?
from gopkg.
Len32 和 Len64 合并成单个函数是不是就可以呀,
if bits.UintSize == 32 是常量比较,我理解编译器可以优化掉,所以单个函数也不会增加额外的计算成本吧。
from gopkg.
个人看法,bits.Len32 和 bits.Len64 这些标准库的函数没必要复制一份?
bits.Len64(x) - 1
跟上面的代码是等价的吧。
from gopkg.
个人看法,bits.Len32 和 bits.Len64 这些标准库的函数没必要复制一份?
bits.Len64(x) - 1
跟上面的代码是等价的吧。
是的。
我是写完之后才意识到这一点。
from gopkg.
并且针对64位操作系统和32位操作系统有不同的优化。
能贴一下你发现的是哪个库会用到吗?
from gopkg.
from gopkg.
from gopkg.
Related Issues (20)
- how to pass variable to gopool function? HOT 1
- GoPool's performance in comparison to other goroutine pool implementations HOT 12
- gc tunner not Use in not web application
- Can 'gopool' support the extended use case of 'errgroup'? HOT 1
- gopool的RegisterPool使用不了 HOT 1
- util/gctuner: test fail randomly HOT 2
- Broken change of golang.org/x/sys causes build failed in go1.16 HOT 2
- Go version: bump minimum version to 1.17
- 如何动态修改熔断器配置 HOT 1
- missing comments of fastrand.Float32 / fastrand.Float64 methods
- Reporting a vulnerability
- a little improvement for `cacheRemap16Byte` HOT 2
- closed
- asynccache的OOM问题
- circuitbreaker Panel 接口添加 State 方法
- logger 中default 的 calldepth = 3 会导致输出Log 无法定位实际输出
- 使用zset(skiplist)时,发现其remove,incr, updateScore比想象中的慢非常多 HOT 1
- gopool中的f func()能否新增可入参的 func()类型? HOT 2
- skipmap在32位系统下运行crash HOT 2
- Test HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gopkg.