Skip to content

Instantly share code, notes, and snippets.

@btrvodka
Created November 8, 2022 08:53
Show Gist options
  • Save btrvodka/8fa99d20a2875e9bc39b64e4dcf3c9d3 to your computer and use it in GitHub Desktop.
Save btrvodka/8fa99d20a2875e9bc39b64e4dcf3c9d3 to your computer and use it in GitHub Desktop.
WaitGroup vs Channels Benchmark
➜ go test -bench=. -benchmem
BenchmarkWaitGroup-4 1432809 831 ns/op 16 B/op 1 allocs/op
BenchmarkChannels-4 1485576 787 ns/op 96 B/op 1 allocs/op
package main
import (
"testing"
"sync"
)
func BenchmarkWaitGroup(b *testing.B) {
for n := 0; n < b.N; n++ {
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
}()
wg.Wait()
}
}
func BenchmarkChannels(b *testing.B) {
for n := 0; n < b.N; n++ {
done := make(chan bool)
go func() {
done <- true
}()
<-done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment