I try to compare using channel for one value vs using mutex. Channel example:
func BenchmarkNewDummy(b *testing.B) {
one := make(chan string, 1)
two := make(chan string, 1)
var wg sync.WaitGroup
wg.Add(2)
go doWork(&wg, one)
go doWork(&wg, two)
wg.Wait()
fmt.Println(<-one)
fmt.Println(<-two)
}
func doWork(wg *sync.WaitGroup, one chan string) {
defer wg.Done()
one <- "hell0"
}
command:
go test -bench=. -benchmem -run BenchmarkNewDummy -cpuprofile cpuCh.out -memprofile memCh.prof
output doesn't provide any useful info
goos: darwin
goarch: amd64
BenchmarkNewDummy-8 hell0
hell0
hell0
hell0
hell0
hell0
hell0
hell0
hell0
hell0
2000000000 0.00 ns/op 0 B/op 0 allocs/op
PASS
ok 0.508s
with mutex situation almost the same:
func BenchmarkNewDummy(b *testing.B) {
one := ""
two := ""
var wg sync.WaitGroup
wg.Add(2)
var mu sync.Mutex
go func() {
mu.Lock()
defer mu.Unlock()
defer wg.Done()
one = "hello"
}()
go func() {
mu.Lock()
defer mu.Unlock()
defer wg.Done()
two = "hello"
}()
wg.Wait()
fmt.Println(one)
fmt.Println(two)
}
output:
goos: darwin
goarch:
BenchmarkNewDummy-8 hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
2000000000 0.00 ns/op 0 B/op 0 allocs/op
PASS
ok 0.521s
memory graph looks almost the same but with mutext bigger memory allocation, but not informative as well:
Are the any to compare channel and mutex memory consumption?