Grokking Concurrency in Go
0% completed
Benchmarking Concurrent Code
func BenchmarkMutexParallel(b *testing.B) { var c MutexCounter b.RunParallel(func(pb *testing.PB) { for pb.Next() { c.Inc() // lock, add one, release the lock } }) }
Part of the output of go test -bench=. -cpu=1,4,16 on one machine with 16 logical CPUs:
BenchmarkMutexParallel 304150128 3.733 ns/op BenchmarkMutexParallel-4 100000000 10.03 ns/op BenchmarkMutexParallel-16 30417865 41.49 ns/op
The benchmark increments a counter that is protected by a mutex. With one goroutine, an increment cost about 4 nanoseconds
.....
.....
.....
Like the course? Get enrolled and start learning!
Reading Progress
0%