Created
July 7, 2017 14:24
-
-
Save Mumakil/a4fe4432ea80268be0e50f5ab9bc9694 to your computer and use it in GitHub Desktop.
Performance difference between inline structs and global structs
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 . | |
BenchmarkInlineStruct-8 2000000000 0.37 ns/op | |
BenchmarkGlobalStruct-8 2000000000 0.30 ns/op | |
PASS | |
ok github.com/Mumakil/testing 1.476s |
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 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