Skip to content

Instantly share code, notes, and snippets.

@ww9
Created November 17, 2018 22:36
Show Gist options
  • Save ww9/7e6f9fb8d3917114f3088abfe2681683 to your computer and use it in GitHub Desktop.
Save ww9/7e6f9fb8d3917114f3088abfe2681683 to your computer and use it in GitHub Desktop.
Benchmark showing that type assertion in Go is pretty fast
package main
import (
"testing"
)
// Why is type assertion so fast?
// https://old.reddit.com/r/golang/comments/9xs0r2/why_is_type_assertion_so_fast/
// go test -bench .
// BenchmarkStructMethod-12 200000000 6.10 ns/op
// BenchmarkInterface-12 200000000 7.43 ns/op
// BenchmarkTypeSwitch-12 200000000 7.14 ns/op
// BenchmarkTypeAssertion-12 200000000 6.53 ns/op
type User interface {
IncAge(i int)
}
type MyUser struct {
Age int
}
func (u *MyUser) IncAge(i int) {
u.Age = u.Age + i
}
func BenchmarkStructMethod(b *testing.B) {
structMap := map[string]*MyUser{"user": &MyUser{}}
structMethod(structMap, b.N)
}
func BenchmarkInterface(b *testing.B) {
interfaceMap := map[string]User{"user": &MyUser{}}
interfaceMethod(interfaceMap, b.N)
}
func BenchmarkTypeSwitch(b *testing.B) {
emptyInterfaceMap := map[string]interface{}{"user": &MyUser{}}
switchMethod(emptyInterfaceMap, b.N)
}
func BenchmarkTypeAssertion(b *testing.B) {
emptyInterfaceMap := map[string]interface{}{"user": &MyUser{}}
assertionMethod(emptyInterfaceMap, b.N)
}
func structMethod(u map[string]*MyUser, n int) {
for k := 0; k < n; k++ {
u["user"].IncAge(1)
}
}
func interfaceMethod(u map[string]User, n int) {
for k := 0; k < n; k++ {
u["user"].IncAge(1)
}
}
func switchMethod(u map[string]interface{}, n int) {
for k := 0; k < n; k++ {
switch v := u["user"].(type) {
case *MyUser:
v.IncAge(1)
}
}
}
func assertionMethod(u map[string]interface{}, n int) {
for k := 0; k < n; k++ {
if user, ok := u["user"].(*MyUser); ok {
user.IncAge(1)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment