Created
April 1, 2014 20:39
-
-
Save OneOfOne/9922689 to your computer and use it in GitHub Desktop.
Benchmarking interface{} vs typed parameters.
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 main | |
import ( | |
"testing" | |
"sync/atomic" | |
) | |
func sumI(nums... interface{}) interface{} { | |
var s uint64 | |
for _, n := range nums { | |
s += uint64(n.(int)) | |
} | |
return s | |
} | |
func sumT(nums... int) uint64 { | |
var s uint64 | |
for _, n := range nums { | |
s += uint64(n) | |
} | |
return s | |
} | |
func BenchmarkInterface(b * testing.B) { | |
var s uint64 | |
for i := 0; i < b.N; i++ { | |
s += sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64) | |
s += sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64) | |
s += sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64) | |
s += sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64) | |
} | |
} | |
func BenchmarkType(b * testing.B) { | |
var s uint64 | |
for i := 0; i < b.N; i++ { | |
s += sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
s += sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
s += sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
s += sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
} | |
} | |
func BenchmarkInterfaceGoroutines(b * testing.B) { | |
var s uint64 | |
ch, done := make(chan uint64), make(chan bool, 4) | |
f := func() { | |
for i := 0; i < b.N; i++ { | |
ch <- sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64) | |
} | |
} | |
l := func() { | |
for i := 0; i < b.N; i++ { | |
s += <- ch | |
} | |
done <- true | |
} | |
for i := 0; i < 4; i++ { | |
go f(); | |
go l(); | |
} | |
for i := 0; i < 4; i++ { | |
<- done | |
} | |
} | |
func BenchmarkTypeGoroutines(b * testing.B) { | |
var s uint64 | |
ch, done := make(chan uint64), make(chan bool, 4) | |
f := func() { | |
for i := 0; i < b.N; i++ { | |
ch <- sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
} | |
} | |
l := func() { | |
for i := 0; i < b.N; i++ { | |
s += <- ch | |
} | |
done <- true | |
} | |
for i := 0; i < 4; i++ { | |
go f(); | |
go l(); | |
} | |
for i := 0; i < 4; i++ { | |
<- done | |
} | |
} | |
func BenchmarkInterfaceGoroutinesBuffered(b * testing.B) { | |
var s uint64 | |
ch, done := make(chan uint64, b.N / 4), make(chan bool, 4) | |
f := func() { | |
for i := 0; i < b.N; i++ { | |
ch <- sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64) | |
} | |
} | |
l := func() { | |
for i := 0; i < b.N; i++ { | |
s += <- ch | |
} | |
done <- true | |
} | |
for i := 0; i < 4; i++ { | |
go f(); | |
go l(); | |
} | |
for i := 0; i < 4; i++ { | |
<- done | |
} | |
} | |
func BenchmarkTypeGoroutinesBuffered(b * testing.B) { | |
var s uint64 | |
ch, done := make(chan uint64, b.N / 4), make(chan bool, 4) | |
f := func() { | |
for i := 0; i < b.N; i++ { | |
ch <- sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
} | |
} | |
l := func() { | |
for i := 0; i < b.N; i++ { | |
s += <- ch | |
} | |
done <- true | |
} | |
for i := 0; i < 4; i++ { | |
go f(); | |
go l(); | |
} | |
for i := 0; i < 4; i++ { | |
<- done | |
} | |
} | |
func BenchmarkInterfaceGoroutinesAtomic(b * testing.B) { | |
var s uint64 | |
done := make(chan bool, 4) | |
f := func() { | |
for i := 0; i < b.N; i++ { | |
atomic.AddUint64(&s, sumI(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).(uint64)) | |
} | |
done <- true | |
} | |
for i := 0; i < 4; i++ { | |
go f(); | |
} | |
for i := 0; i < 4; i++ { | |
<- done | |
} | |
} | |
func BenchmarkTypeGoroutinesAtomic(b * testing.B) { | |
var s uint64 | |
done := make(chan bool, 4) | |
f := func() { | |
for i := 0; i < b.N; i++ { | |
atomic.AddUint64(&s, sumT(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) | |
} | |
done <- true | |
} | |
for i := 0; i < 4; i++ { | |
go f(); | |
} | |
for i := 0; i < 4; i++ { | |
<- done | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment