Skip to content

Instantly share code, notes, and snippets.

@mwf
Last active May 22, 2019 14:33
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 mwf/e0593c7d026e3f3eb02b966abe80dd72 to your computer and use it in GitHub Desktop.
Save mwf/e0593c7d026e3f3eb02b966abe80dd72 to your computer and use it in GitHub Desktop.
Go escape analysis and allocations test
// go test -gcflags '-m' -benchmem -bench .
// BenchmarkAllocsFoo-4 5000000 387 ns/op 32 B/op 1 allocs/op
// BenchmarkAllocsFooPointer-4 3000000 481 ns/op 80 B/op 4 allocs/op
package main
import (
"fmt"
"io/ioutil"
"testing"
)
type F func()
type CallsArgs struct {
Concurrency int
CallsNumber int
Fn F
}
func Foo(args CallsArgs) {}
func FooPointer(args *CallsArgs) {}
func BenchmarkAllocsFoo(b *testing.B) {
for i := 0; i < b.N; i++ {
args := CallsArgs{Concurrency: 1, CallsNumber: 42, Fn: func() {}}
Foo(args)
fmt.Fprintln(ioutil.Discard, args)
}
}
func BenchmarkAllocsFooPointer(b *testing.B) {
for i := 0; i < b.N; i++ {
args := &CallsArgs{Concurrency: 1, CallsNumber: 42, Fn: func() {}}
FooPointer(args)
fmt.Fprintln(ioutil.Discard, args)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment