Skip to content

Commit

Permalink
Merge pull request #1 from ORBAT/feature/assert-reflect
Browse files Browse the repository at this point in the history
Add some type assertion and reflection benchmarks
  • Loading branch information
mna committed Dec 19, 2014
2 parents 9e27681 + ee9479e commit dd60698
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
42 changes: 42 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gocostmodel

import (
"testing"
)

type assertStr struct {
a1 int
}

var (
assertPiface interface{} = &assertStr{}
assertIface interface{} = assertStr{}
assertStra assertStr
assertStrp *assertStr
)

var ok bool

func BenchmarkIfacePtrTypeAssertShort(b *testing.B) {
for i := 0; i < b.N; i++ {
assertStrp = assertPiface.(*assertStr)
}
}

func BenchmarkIfacePtrTypeAssertWithOk(b *testing.B) {
for i := 0; i < b.N; i++ {
assertStrp, ok = assertPiface.(*assertStr)
}
}

func BenchmarkIfaceTypeAssertShort(b *testing.B) {
for i := 0; i < b.N; i++ {
assertStra = assertIface.(assertStr)
}
}

func BenchmarkIfaceTypeAssertWithOk(b *testing.B) {
for i := 0; i < b.N; i++ {
assertStra, ok = assertIface.(assertStr)
}
}
41 changes: 41 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gocostmodel

import (
"reflect"
"testing"
)

type reflectStr struct {
a1 int
}

var (
reflectStra = reflectStr{}
reflectStrp = &reflectStr{}
reflectStrVal = reflect.ValueOf(reflectStr{1})
reflectStrPVal = reflect.ValueOf(&reflectStr{1})
)

func BenchmarkReflFromVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStra = reflectStrVal.Interface().(reflectStr)
}
}

func BenchmarkReflToVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStrVal = reflect.ValueOf(reflectStra)
}
}

func BenchmarkReflPtrFromVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStrp = reflectStrPVal.Interface().(*reflectStr)
}
}

func BenchmarkReflPtrToVal(b *testing.B) {
for i := 0; i < b.N; i++ {
reflectStrPVal = reflect.ValueOf(reflectStrp)
}
}

0 comments on commit dd60698

Please sign in to comment.