Re: [go-nuts] Bitmask is slower than modulo calculation

2024-05-14 Thread 'Keith Randall' via golang-nuts
Your test is benchmarking int->any conversions more than it is testing the underlying modulo/mask difference. When you pass an integer to Enqueue, it is converted to any, which (usually) involves an allocation. That will swamp the cost of any single arithmetic operation. There are a few ways to

Re: [go-nuts] Bitmask is slower than modulo calculation

2024-05-12 Thread robert engels
Hi. This is still not correct. Use the “for.. in b.N” as discussed in the blog in order to understand the per op difference - which will be more accurate for a microbenchmark timing, But, if you really want to make a case that bit mask is slower than mod, then a simpler test would be better - y

Re: [go-nuts] Bitmask is slower than modulo calculation

2024-05-12 Thread Yuta MIYAKE
Thank you. This is benchstat result. new test code follows: ❯ go test -test.bench BenchmarkTestSingleModulo -count=10 -cpu=1 > modulo.txt ❯ go test -test.bench BenchmarkTestSingleBitMask -count=10 -cpu=1 > bitmask.txt ❯ benchstat modulo.txt bitmask.txt goos: darwin goarch: arm64 pkg: ringbuffe

Re: [go-nuts] Bitmask is slower than modulo calculation

2024-05-12 Thread leon
Thank you so much. 2024年5月13日月曜日 12:58:09 UTC+9 Kurtis Rader: > On my Apple Studio system the difference in speed is barely measurable and > sometimes the bitmask implementation is faster: > > macpro ~/experiment > go run ring.go > 10 ops in 5092 ms > 10 ops in 5140 ms > mac

Re: [go-nuts] Bitmask is slower than modulo calculation

2024-05-12 Thread Kurtis Rader
On my Apple Studio system the difference in speed is barely measurable and sometimes the bitmask implementation is faster: macpro ~/experiment > go run ring.go 10 ops in 5092 ms 10 ops in 5140 ms macpro ~/experiment > go run ring.go 10 ops in 5080 ms 10 ops in

Re: [go-nuts] Bitmask is slower than modulo calculation

2024-05-12 Thread robert engels
Use the Go benchmarking facilities, see https://dave.cheney.net/2013/06/30/how-to-write-benchmarks-in-go > On May 11, 2024, at 9:57 PM, leon wrote: > > I'm trying to prove an optimization technique for ring buffer is effective. > One of the technique is using bitmask instead of modulo to calcu

[go-nuts] Bitmask is slower than modulo calculation

2024-05-11 Thread leon
I'm trying to prove an optimization technique for ring buffer is effective. One of the technique is using bitmask instead of modulo to calculate a wrap around. However, in my environment, modulo is slightly faster in a test where 1 billion items are enqueued /dequeued by a single goroutine. What