-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ORBAT/feature/assert-reflect
Add some type assertion and reflection benchmarks
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
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
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) | ||
} | ||
} |
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
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) | ||
} | ||
} |