Created
March 4, 2014 09:13
-
-
Save atotto/9342938 to your computer and use it in GitHub Desktop.
golang channel benchmark
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
package chan_test | |
import ( | |
"testing" | |
) | |
func BenchmarkStructChan(b *testing.B) { | |
ch := make(chan struct{}) | |
go func() { | |
for { | |
<-ch | |
} | |
}() | |
for i := 0; i < b.N; i++ { | |
ch <- struct{}{} | |
} | |
} | |
func BenchmarkBoolChan(b *testing.B) { | |
ch := make(chan bool) | |
go func() { | |
for { | |
<-ch | |
} | |
}() | |
for i := 0; i < b.N; i++ { | |
ch <- true | |
} | |
} | |
func BenchmarkIntChan(b *testing.B) { | |
ch := make(chan int) | |
go func() { | |
for { | |
<-ch | |
} | |
}() | |
for i := 0; i < b.N; i++ { | |
ch <- 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To add onto that, if you were really trying to send one item at a time, you should be using a mutex from sync.
Here's what I got on a 2 vCPU (Haswell 3GHz) VPS with 1.6.2 and 1.9.2.