Created
November 8, 2022 08:53
-
-
Save btrvodka/8fa99d20a2875e9bc39b64e4dcf3c9d3 to your computer and use it in GitHub Desktop.
WaitGroup vs Channels Benchmark
This file contains hidden or 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
➜ 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 |
This file contains hidden or 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
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