Skip to content

Instantly share code, notes, and snippets.

@jzila
Last active May 5, 2017 23:02
Show Gist options
  • Save jzila/4c8d70742ea72b2404046186d67bbb1f to your computer and use it in GitHub Desktop.
Save jzila/4c8d70742ea72b2404046186d67bbb1f to your computer and use it in GitHub Desktop.
Various Go Dispatch Tests
package test
import (
"testing"
)
func sum(a, i uint64) uint64 {
return a + i
}
func BenchmarkStaticDispatch(b *testing.B) {
num := uint64(0)
for i := 0; i < b.N; i++ {
num = sum(num, uint64(i))
}
b.Log(num)
}
func BenchmarkDynamicDispatch(b *testing.B) {
num := uint64(0)
f := sum
for i := 0; i < b.N; i++ {
num = f(num, uint64(i))
}
b.Log(num)
}
func BenchmarkClosure(b *testing.B) {
num := uint64(0)
f := func(i uint64) {
num = num + i
}
for i := 0; i < b.N; i++ {
f(uint64(i))
}
b.Log(num)
}
func BenchmarkNestedClosure(b *testing.B) {
num := uint64(0)
f := func(i uint64) {
num = num + i
}
for i := 0; i < b.N; i++ {
func() {
f(uint64(i))
}()
}
b.Log(num)
}
func BenchmarkClosureWithEmptyCall(b *testing.B) {
num := uint64(0)
f := func(i uint64) {
num = num + i
}
g := func() {}
for i := 0; i < b.N; i++ {
g()
f(uint64(i))
}
b.Log(num)
}
func BenchmarkNestedClosureWithEmptyCall(b *testing.B) {
num := uint64(0)
f := func(i uint64) {
num = num + i
}
g := func() {}
for i := 0; i < b.N; i++ {
func() {
g()
f(uint64(i))
}()
}
b.Log(num)
}
func BenchmarkClosureSkipEmptyCall(b *testing.B) {
num := uint64(0)
f := func(i uint64) {
num = num + i
}
var g func() = nil
for i := 0; i < b.N; i++ {
if g != nil {
g()
}
f(uint64(i))
}
b.Log(num)
}
imac 13:44:46 test ➜ go test -bench . -v
BenchmarkStaticDispatch-4 2000000000 0.38 ns/op
--- BENCH: BenchmarkStaticDispatch-4
test_test.go:16: 0
test_test.go:16: 4950
test_test.go:16: 49995000
test_test.go:16: 499999500000
test_test.go:16: 4999999950000000
test_test.go:16: 1999999999000000000
BenchmarkDynamicDispatch-4 1000000000 2.42 ns/op
--- BENCH: BenchmarkDynamicDispatch-4
test_test.go:25: 0
test_test.go:25: 4950
test_test.go:25: 49995000
test_test.go:25: 499999500000
test_test.go:25: 4999999950000000
test_test.go:25: 499999999500000000
BenchmarkClosure-4 1000000000 2.02 ns/op
--- BENCH: BenchmarkClosure-4
test_test.go:36: 0
test_test.go:36: 4950
test_test.go:36: 49995000
test_test.go:36: 499999500000
test_test.go:36: 4999999950000000
test_test.go:36: 499999999500000000
BenchmarkNestedClosure-4 500000000 3.47 ns/op
--- BENCH: BenchmarkNestedClosure-4
test_test.go:49: 0
test_test.go:49: 4950
test_test.go:49: 49995000
test_test.go:49: 499999500000
test_test.go:49: 4999999950000000
test_test.go:49: 124999999750000000
BenchmarkClosureWithEmptyCall-4 500000000 2.95 ns/op
--- BENCH: BenchmarkClosureWithEmptyCall-4
test_test.go:62: 0
test_test.go:62: 4950
test_test.go:62: 49995000
test_test.go:62: 499999500000
test_test.go:62: 4999999950000000
test_test.go:62: 124999999750000000
BenchmarkNestedClosureWithEmptyCall-4 300000000 4.59 ns/op
--- BENCH: BenchmarkNestedClosureWithEmptyCall-4
test_test.go:77: 0
test_test.go:77: 4950
test_test.go:77: 49995000
test_test.go:77: 499999500000
test_test.go:77: 4999999950000000
test_test.go:77: 44999999850000000
BenchmarkClosureSkipEmptyCall-4 1000000000 2.08 ns/op
--- BENCH: BenchmarkClosureSkipEmptyCall-4
test_test.go:92: 0
test_test.go:92: 4950
test_test.go:92: 49995000
test_test.go:92: 499999500000
test_test.go:92: 4999999950000000
test_test.go:92: 499999999500000000
PASS
ok github.com/jzila/test 13.789s
@jzila
Copy link
Author

jzila commented May 5, 2017

@songgao: oddly enough worse than the Closure:

BenchmarkDynamicDispatch-4                      1000000000               2.42 ns/op
--- BENCH: BenchmarkDynamicDispatch-4
        test_test.go:25: 0
        test_test.go:25: 4950
        test_test.go:25: 49995000
        test_test.go:25: 499999500000
        test_test.go:25: 4999999950000000
        test_test.go:25: 499999999500000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment