Skip to content

Instantly share code, notes, and snippets.

@Mumakil
Created July 7, 2017 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mumakil/a4fe4432ea80268be0e50f5ab9bc9694 to your computer and use it in GitHub Desktop.
Save Mumakil/a4fe4432ea80268be0e50f5ab9bc9694 to your computer and use it in GitHub Desktop.
Performance difference between inline structs and global structs
# go test -bench .
BenchmarkInlineStruct-8 2000000000 0.37 ns/op
BenchmarkGlobalStruct-8 2000000000 0.30 ns/op
PASS
ok github.com/Mumakil/testing 1.476s
package testing_test
import "testing"
func BenchmarkInlineStruct(b *testing.B) {
accu := 0
for i := 0; i < b.N; i++ {
type LocalStruct struct {
Foo int
Bar string
}
ls := LocalStruct{
Foo: 1,
Bar: "foo",
}
accu += ls.Foo
}
}
type GlobalStruct struct {
Foo int
Bar string
}
func BenchmarkGlobalStruct(b *testing.B) {
accu := 0
for i := 0; i < b.N; i++ {
gs := GlobalStruct{
Foo: 1,
Bar: "foo",
}
accu += gs.Foo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment