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
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
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
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
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
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
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