Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cachebenches
- import (
- "math/rand"
- "runtime"
- "sort"
- "sync/atomic"
- "testing"
- )
- // false sharing benchmark ----------------------------------------------------
- type countersUnpadded struct {
- values [8]uint64
- }
- type paddedCounter struct {
- value uint64
- _ [56]byte
- }
- type countersPadded struct {
- values [8]paddedCounter
- }
- func BenchmarkFalseSharing(b *testing.B) {
- runtime.GOMAXPROCS(runtime.NumCPU())
- b.Run("with_false_sharing", func(b *testing.B) {
- var c countersUnpadded
- var goroutineID uint64
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- id := atomic.AddUint64(&goroutineID, 1) - 1
- target := &c.values[id%uint64(len(c.values))]
- for pb.Next() {
- atomic.AddUint64(target, 1)
- }
- })
- })
- b.Run("with_padding", func(b *testing.B) {
- var c countersPadded
- var goroutineID uint64
- b.ResetTimer()
- b.RunParallel(func(pb *testing.PB) {
- id := atomic.AddUint64(&goroutineID, 1) - 1
- target := &c.values[id%uint64(len(c.values))].value
- for pb.Next() {
- atomic.AddUint64(target, 1)
- }
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment