Last active
May 22, 2019 14:33
-
-
Save mwf/e0593c7d026e3f3eb02b966abe80dd72 to your computer and use it in GitHub Desktop.
Go escape analysis and allocations test
This file contains hidden or 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 -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