Created
July 6, 2020 20:21
-
-
Save jwbee/8d2d8e5980b232cde638b5f83cc5f3e3 to your computer and use it in GitHub Desktop.
Go counter benchmarks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* result */ | |
$ go test -test.bench=. -test.cpu=1,2,4,8 -test.benchtime=10s . | |
goos: linux | |
goarch: amd64 | |
BenchmarkAdd 1000000000 6.97 ns/op | |
BenchmarkAdd-2 532704848 21.1 ns/op | |
BenchmarkAdd-4 675975466 17.8 ns/op | |
BenchmarkAdd-8 767997027 15.7 ns/op | |
BenchmarkCASPause 1000000000 8.92 ns/op | |
BenchmarkCASPause-2 1000000000 8.92 ns/op | |
BenchmarkCASPause-4 1000000000 8.92 ns/op | |
BenchmarkCASPause-8 1000000000 9.21 ns/op | |
BenchmarkCAS 1000000000 8.92 ns/op | |
BenchmarkCAS-2 237693028 50.6 ns/op | |
BenchmarkCAS-4 185520495 64.7 ns/op | |
BenchmarkCAS-8 100000000 104 ns/op | |
PASS | |
/* con.s */ | |
// +build amd64 | |
TEXT ·procyield(SB), $0 | |
JMP runtime·procyield(SB) | |
/* con_test.go */ | |
package con | |
import ( | |
"sync/atomic" | |
"testing" | |
) | |
func BenchmarkAdd(b *testing.B) { | |
c := uint64(0) | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
atomic.AddUint64(&c, 42) | |
} | |
}) | |
} | |
func procyield() | |
func BenchmarkCASPause(b *testing.B) { | |
c := uint64(0) | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
cp := c + 42 | |
stop := atomic.CompareAndSwapUint64(&c, c, cp) | |
for !stop { | |
procyield() | |
cp = c + 42 | |
stop = atomic.CompareAndSwapUint64(&c, c, cp) | |
} | |
} | |
}) | |
} | |
func BenchmarkCAS(b *testing.B) { | |
c := uint64(0) | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
for stop := false; !stop; { | |
cp := c + 42 | |
stop = atomic.CompareAndSwapUint64(&c, c, cp) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment