Last active
January 3, 2016 23:19
-
-
Save sdboyer/8534721 to your computer and use it in GitHub Desktop.
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 benchtest | |
import ( | |
"testing" | |
) | |
type iface_foo interface { | |
foo() | |
} | |
type struct_foo struct { | |
} | |
func (f struct_foo) foo() { | |
} | |
var foovar = struct_foo{} | |
var pfoovar = &struct_foo{} | |
func BenchmarkTypeCast(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var _ iface_foo = foovar | |
} | |
} | |
func BenchmarkPointerTypeCast(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var _ iface_foo = pfoovar | |
} | |
} | |
func BenchmarkSliceAppend(b *testing.B) { | |
var appender = make([]struct_foo, b.N) | |
for i := 0; i < b.N; i++ { | |
appender = append(appender, foovar) | |
} | |
} | |
func BenchmarkSliceGrowAndAppend(b *testing.B) { | |
// Make length small to force underlying array to grow on appends | |
var appender = make([]struct_foo, 2) | |
for i := 0; i < b.N; i++ { | |
appender = append(appender, foovar) | |
} | |
} |
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
$ go test -bench=Bench | |
testing: warning: no tests to run | |
PASS | |
BenchmarkTypeCast 100000000 11.1 ns/op | |
BenchmarkPointerTypeCast 2000000000 0.84 ns/op | |
BenchmarkSliceAppend 500000000 2.94 ns/op | |
BenchmarkSliceGrowAndAppend 500000000 4.23 ns/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment