Skip to content

Instantly share code, notes, and snippets.

@trustmaster
Created August 16, 2013 16:30
Show Gist options
  • Save trustmaster/6251390 to your computer and use it in GitHub Desktop.
Save trustmaster/6251390 to your computer and use it in GitHub Desktop.
Simple benchmark for Go channel performance on different data types
package chanperf
import (
"testing"
)
func BenchmarkInt(b *testing.B) {
fin := make(chan bool)
comm := make(chan int)
go func() {
for i := 0; i < b.N; i++ {
comm <- i
}
fin <- true
}()
go func() {
for i := 0; i < b.N; i++ {
<-comm
}
fin <- true
}()
<-fin
<-fin
}
func BenchmarkString(b *testing.B) {
fin := make(chan bool)
comm := make(chan string)
go func() {
for i := 0; i < b.N; i++ {
comm <- "testing foobars until this string gets extremely long and ends up in a crash with undesired consequences nobody could ever escape unless they took careful measures"
}
fin <- true
}()
go func() {
for i := 0; i < b.N; i++ {
<-comm
}
fin <- true
}()
<-fin
<-fin
}
type DataPart struct {
name, value string
count int
}
type ComplexData struct {
i, j, k int
s, t string
part DataPart
}
func BenchmarkComplex(b *testing.B) {
fin := make(chan bool)
comm := make(chan ComplexData)
test := ComplexData{i: 123789, j: 456032, k: 987562, s: "testing foobards", t: "testing foobars until this string gets extremely long and ends up in a crash with undesired consequences nobody could ever escape unless they took careful measures", part: DataPart{name: "John", value: "A lot of", count: 1337}}
go func() {
for i := 0; i < b.N; i++ {
comm <- test
}
fin <- true
}()
go func() {
for i := 0; i < b.N; i++ {
<-comm
}
fin <- true
}()
<-fin
<-fin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment