Created
January 19, 2023 10:57
-
-
Save yyforyongyu/faeceab3de32e208cbe83b9486f5b3cd to your computer and use it in GitHub Desktop.
Benchmark on read and write for `atomic.Bool` and mutex `bool`
This file contains 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
func BenchmarkBoolAmoticWrite(b *testing.B) { | |
type foo struct { | |
x atomic.Bool | |
} | |
f := &foo{} | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
f.x.Store(true) | |
} | |
}) | |
} | |
func BenchmarkBoolRWMutexWrite(b *testing.B) { | |
type foo struct { | |
x bool | |
mu sync.RWMutex | |
} | |
f := &foo{} | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
f.mu.Lock() | |
f.x = true | |
f.mu.Unlock() | |
} | |
}) | |
} | |
func BenchmarkBoolAmoticRead(b *testing.B) { | |
type foo struct { | |
x atomic.Bool | |
} | |
f := &foo{} | |
f.x.Store(true) | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
_ = f.x.Load() | |
} | |
}) | |
} | |
func BenchmarkBoolRWMutexRead(b *testing.B) { | |
type foo struct { | |
x bool | |
mu sync.RWMutex | |
} | |
f := &foo{x: true} | |
b.RunParallel(func(pb *testing.PB) { | |
for pb.Next() { | |
f.mu.Lock() | |
_ = f.x | |
f.mu.Unlock() | |
} | |
}) | |
} |
Author
yyforyongyu
commented
Jan 19, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment