Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 22, 2023 09:34
Show Gist options
  • Save yumed15/b48d6d83bc80407c78c209c7895b510d to your computer and use it in GitHub Desktop.
Save yumed15/b48d6d83bc80407c78c209c7895b510d to your computer and use it in GitHub Desktop.
type T struct {
X int32 // 4B
}
var global interface{}
func BenchmarkAllocOnHeap(b *testing.B) {
b.ReportAllocs()
for i := 0; i <= b.N; i++ {
global = &T{}
}
}
func BenchmarkAllocOnStack(b *testing.B) {
b.ReportAllocs()
for i := 0; i <= b.N; i++ {
local := T{}
_ = local
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment